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

Everything else that doesn't fall into one of the other PB categories.
registrymechanic22
Enthusiast
Enthusiast
Posts: 176
Joined: Sun Jun 28, 2009 7:07 pm
Location: RUS

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

Post 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?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

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

Post 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.
User avatar
Demivec
Addict
Addict
Posts: 4283
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

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

Post 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.
registrymechanic22
Enthusiast
Enthusiast
Posts: 176
Joined: Sun Jun 28, 2009 7:07 pm
Location: RUS

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

Post 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.
Fred
Administrator
Administrator
Posts: 18553
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post 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).
Post Reply