Shamil Salakhetdinov
shamil at users.mns.ru
Mon Oct 31 05:17:09 CST 2005
Gustav,
And finally here is managed C++, which runs instantaneously:
#include "stdafx.h"
#using <mscorlib.dll>
#using <Microsoft.VisualBasic.dll>
using namespace System;
static long ArrayTimeLocal(long lngLoopMax, long& lngTotalCnt);
int _tmain()
{
long lngTotalCnt = 0;
long lngResult = 0;
lngResult = ArrayTimeLocal(1000000, lngTotalCnt);
Console::WriteLine(String::Format(
S"Final Result = {0:#,##0}, Final Counter = {1:#,##0}",
__box(lngResult), __box(lngTotalCnt)));
}
static long ArrayTimeLocal(long lngLoopMax, long& lngTotalCnt) {
const long lngItems = 100;
long alngTmp[lngItems-1][2];
long lngLoop;
long lngItem;
long lngResult=0;
double dblStart;
double dblStop;
long lngSeconds;
Console::WriteLine("Array looping test started...");
dblStart = Microsoft::VisualBasic::DateAndTime::Timer;
lngTotalCnt = 0;
for (lngLoop=0; lngLoop < lngLoopMax; lngLoop++) {
for (lngItem=0; lngItem < lngItems; lngItem++) {
alngTmp[lngItem][0] = lngLoop * 10;
if (alngTmp[lngItem][0] / 10 == 100) lngResult = 1;
else lngResult = 0;
lngTotalCnt++;
}
}
dblStop = Microsoft::VisualBasic::DateAndTime::Timer;
Console::WriteLine("Array looping test ended.");
lngSeconds = (long)(dblStop - dblStart);
return lngSeconds;
}
Output
---------
Array looping test started...
Array looping test ended.
Final Result = 0, Final Counter = 100,000,000
And it takes ~18 seconds on my PC to run this test ADDITIONALLY looped 100
times....
Shamil
----- Original Message -----
From: "Shamil Salakhetdinov" <shamil at users.mns.ru>
To: "Access Developers discussion and problem solving"
<accessd at databaseadvisors.com>
Sent: Monday, October 31, 2005 1:34 PM
Subject: Re: [AccessD] Array faster in DLL?
> Gustav,
>
> I think if you say that you have forgotten 99% Fortran then you can
convert
> your code into C++ with same efforts as on Fortran. I mean - if your
> code is just "arrays shuffling" as you say then VB(A)-> C++ conversion is
> almost line by line...
>
> I did recheck - your code runs just in 5 seconds on VB6 with array bounds
> checks and integer overflow checks switched off. These are 100,000,000
> loops.
>
> I did also test your sample code on C# and VB.NET - and it runs in 5-6
> seconds
> with switched off integer overflow checks (it looks that there is no way
to
> switch off array bounds checks for C# and VB.NET) .
>
> Here is VB.NETcode I used for testing:
>
> VB.NET
> -----------
> Module testArrays
>
> Sub Main()
> Dim lngResult As Long = 0
> Dim lngCnt As Long = 0
> Dim lngTotalCnt As Long = 0
> lngResult = ArrayTimeLocal(1000000, lngTotalCnt)
> lngTotalCnt += lngCnt
> Console.WriteLine(String.Format( _
> "Final Result = {0:#,##0}, Final Counter = {1:#,##0}", _
> lngResult, lngTotalCnt))
> End Sub
>
> Function ArrayTimeLocal(ByVal lngLoopMax As Long, ByRef lngTotalCnt As
> Long) As Long
>
> Const lngItems As Long = 100
>
> Dim alngTmp(lngItems - 1, 1) As Long
> Dim lngLoop As Long
> Dim lngItem As Long
> Dim lngResult As Long
> Dim lngSeconds As Long
> Dim dblStart As Double
> Dim dblStop As Double
> Dim lngCnt As Long = 0
> dblStart = Timer
>
> For lngLoop = 0 To lngLoopMax - 1
> For lngItem = 0 To lngItems - 1
> alngTmp(lngItem, 1) = lngLoop * 10
> If alngTmp(lngItem, 1) / 10 = 100 Then
> lngResult = 1
> Else
> lngResult = 0
> End If
> lngCnt += 1
> Next
> Next
>
> dblStop = Timer
> lngSeconds = CLng(dblStop - dblStart)
>
> lngTotalCnt = lngCnt
> ArrayTimeLocal = lngSeconds
>
> End Function
>
> End Module
>
> Output
> ----------
> Final Result = 5, Final Counter = 100,000,000
>
> C#
> ===
> using System;
> using Microsoft.VisualBasic;
>
> namespace testArraysCS
> {
> class TestArrays
> {
> [STAThread]
> unsafe static void Main(string[] args)
> {
> long lngTotalCnt = 0;
> long lngResult = 0;
>
> lngResult = ArrayTimeLocal(1000000, ref lngTotalCnt);
> Console.WriteLine(String.Format(
> "Final Result = {0:#,##0}, Final Counter = {1:#,##0}",
> lngResult, lngTotalCnt));
> }
>
> unsafe static long ArrayTimeLocal(long lngLoopMax, ref long lngTotalCnt)
{
> const long lngItems = 100;
> long[,] alngTmp = new long[lngItems,1] ;
> long lngLoop;
> long lngItem;
> long lngResult=0;
> double dblStart;
> double dblStop;
> long lngSeconds;
>
> Console.WriteLine("Array looping test started...");
> dblStart = Microsoft.VisualBasic.DateAndTime.Timer;
> lngTotalCnt = 0;
> for (lngLoop=0; lngLoop < lngLoopMax; lngLoop++) {
> for (lngItem=0; lngItem < lngItems; lngItem++) {
> alngTmp[lngItem,0] = lngLoop * 10;
> if (alngTmp[lngItem,0] / 10 == 100) lngResult = 1;
> else lngResult = 0;
> lngTotalCnt++;
> }
> }
> dblStop = Microsoft.VisualBasic.DateAndTime.Timer;
> Console.WriteLine("Array looping test ended.");
> lngSeconds = (long)(dblStop - dblStart);
> return lngSeconds;
> }
>
> }
> }
>
> Output
> ---------
> Array looping test started...
> Array looping test ended.
> Final Result = 6, Final Counter = 100,000,000
>
> Recapitulation:
> ===========
>
> C++ & Fortran (and COBOL?) rule and rock forever! :)
>
> Shamil
>
<<< tail skipped>>