Page 1 of 1

"For ... Next" faster than "CopyArray ()"

Posted: Mon Aug 09, 2010 8:35 am
by registrymechanic22
I was glad to rise "CopyArray ()", but it turned out, this command is slower ...
:( :(

Code: Select all

;****************************************************************

Procedure.s RandomTextALL(ChisloZnakov)
For x=1 To ChisloZnakov   
        Select Random(2)
            Case 0
              Bukva=Random(25)+65
            Case 1
              Bukva=Random(25)+97
            Case 2
              Bukva=Random(63)+192
        EndSelect
    Slovo$=Slovo$+Chr(Bukva)
Next   
ProcedureReturn Slovo$
EndProcedure

Procedure PerformanceQuery(TIP)
Static ChastotaSchetchika2.d
Static ZnachenieSchetchikaIN2.d
    Select TIP
        Case 0
            QueryPerformanceFrequency_(@ChastotaSchetchika2.d)
            QueryPerformanceCounter_(@ZnachenieSchetchikaIN2.d)
            
        Case 1
            QueryPerformanceCounter_(@ZnachenieSchetchikaOUT2.d)
            ProshloMikroSekund2=(ZnachenieSchetchikaOUT2-ZnachenieSchetchikaIN2)/ChastotaSchetchika2*1000000
            ProcedureReturn ProshloMikroSekund2
            
    EndSelect
EndProcedure

;****************************************************************

#fact=2111111
Dim MyArray1.s (#fact)
Dim MyArray2.s (0)


For x=0 To #fact
;     MyArray1(x)=RandomTextALL(10)
    MyArray1(x)="999888777333222141144564312"
Next

;****************************************************************
;                           Result 1
        PerformanceQuery(0) 
    
            ReDim MyArray2(ArraySize(MyArray1()))
        
            For x=0 To #fact
                MyArray2(x)=MyArray1(x)
            Next
            
        MS=PerformanceQuery(1) 
        par1.d=MS
            
        Debug "FOR - NEXT"
        Debug Str(MS/1000)+"  milliseconds"
        Debug Str(MS)+"  mikroseund"
        Debug "------------------------------------------------"
        Debug ""
        
;****************************************************************

                Delay (500)
                FreeArray(MyArray2())
                Dim MyArray2.s (0)
                
;****************************************************************
;                           Result 2
        PerformanceQuery(0)
    
            CopyArray(MyArray1(), MyArray2())
    
        MS=PerformanceQuery(1) 
        par2.d=MS
        
        Debug "CopyArray (...)"
        Debug Str(MS/1000)+"  milliseconds"
        Debug Str(MS)+"  mikroseund"
        Debug "------------------------------------------------"
        Debug ""
        
;****************************************************************
        
        Debug "on  " + StrF(100-(par1*100/par2),1) + " %   slower"

;****************************************************************
        
; For x=0 To #fact/10
;     Debug MyArray1(x)
;     Debug MyArray2(x)
;     Debug "****************"
; Next

;****************************************************************
or something is wrong doing?

Re: "For ... Next" faster than "CopyArray ()"

Posted: Mon Aug 09, 2010 9:04 am
by Trond
or something is wrong doing?
You can't do speed testing with the debugger on, the results will be wrong.

But you're right, it seems like CopyArray() is slower.

Re: "For ... Next" faster than "CopyArray ()"

Posted: Mon Aug 09, 2010 9:25 am
by Demivec
registrymechanic22 wrote:I was glad to rise "CopyArray ()", but it turned out, this command is slower ... or something is wrong doing?
According to my tests (without the debugger) based on your example CopyArray() is faster than For/Next in all but one case, the case you tested.

CopyArray() is faster for non-string elements or structured elements (with either strings or non-string sub-elements). It is not faster when the array elements are strings.

Re: "For ... Next" faster than "CopyArray ()"

Posted: Mon Aug 09, 2010 10:04 am
by registrymechanic22
Trond wrote:You can't do speed testing with the debugger on, the results will be wrong.
Demivec wrote:CopyArray() is faster for non-string elements or structured elements (with either strings or non-string sub-elements)
ok,thanks for the clarification...
Yours respectfully.

Re: "For ... Next" faster than "CopyArray ()"

Posted: Mon Aug 09, 2010 10:27 am
by Fred
I tuned a bit the CopyStructure() command used internally by CopyArray() and it is now faster for small structure (as a single string is considered as a structure with a string inside).