Code: Select all
;DisableDebugger
CompilerIf #PB_Compiler_Debugger
End
CompilerEndIf
SetPriorityClass_(GetCurrentProcess_(), #REALTIME_PRIORITY_CLASS)
SetThreadPriority_(GetCurrentThread_(), #THREAD_PRIORITY_TIME_CRITICAL)
#code = 0 ; While ... Wend
;#code = 1 ; For ... Next
Global.q t1, t2, t3, t4, t5, t6, maxfreq
Global.d d1, d2, d3
Global howmannytimes, k
howmannytimes = 10000000
QueryPerformanceFrequency_(@maxfreq)
!align 4
!align 4
!align 4
!align 4
QueryPerformanceCounter_(@t1)
!align 4
!align 4
!align 4
!align 4
CompilerIf #code
For k = 1 To howmannytimes
Next
CompilerElse
k = 0
While (k < howmannytimes)
k = k + 1
Wend
CompilerEndIf
QueryPerformanceCounter_(@t2)
!align 4
!align 4
!align 4
!align 4
QueryPerformanceCounter_(@t3)
!align 4
!align 4
!align 4
!align 4
CompilerIf #code
For k = 1 To howmannytimes
Next
CompilerElse
k = 0
While (k < howmannytimes)
k = k + 1
Wend
CompilerEndIf
QueryPerformanceCounter_(@t4)
!align 4
!align 4
!align 4
!align 4
QueryPerformanceCounter_(@t5)
!align 4
!align 4
!align 4
!align 4
CompilerIf #code
For k = 1 To howmannytimes
Next
CompilerElse
k = 0
While (k < howmannytimes)
k = k + 1
Wend
CompilerEndIf
QueryPerformanceCounter_(@t6)
t2 - t1
t4 - t3
t6 - t5
d1 = t2 / maxfreq * 1000
d2 = t4 / maxfreq * 1000
d3 = t6 / maxfreq * 1000
CompilerIf #code
MessageRequester("Result: For ... Next", StrD(d1) + " ms" + #LF$ + StrD(d2) + " ms" + #LF$ + StrD(d3) + " ms")
CompilerElse
MessageRequester("Result: While ... Wend", StrD(d1) + " ms" + #LF$ + StrD(d2) + " ms" + #LF$ + StrD(d3) + " ms")
CompilerEndIf
[/size]
My result:
(CPU: AMD Athlon II X4 630 Propus 2.8GHz)
10,000,000 loops of "While ... Wend" ; 23 ~ 28 ms
10,000,000 loops of "For ... Next" ; 24 ~ 26 ms
Thus, the difference is negligible.
freepurebasic wrote:so my advice is : use while instread for in ANY language!
My conclusion is that the "While" and "For" are almost the same speed in PureBasic.
But I don't know about the others.
---------------------------
Result: While ... Wend
---------------------------
26.4243081174 ms
23.8658823957 ms
28.5692734691 ms
23.3605109029 ms
28.5634068017 ms
26.8682192849 ms
28.5983274411 ms
23.3795077307 ms
25.2926000372 ms
24.5992158221 ms
28.5745814063 ms
23.6133363319 ms
24.4567396136 ms
25.4708349804 ms
27.0366764491 ms
24.8836095090 ms
24.8872412555 ms
25.7912667671 ms
---------------------------
Result: For ... Next
---------------------------
24.9914444434 ms
26.5922065514 ms
24.9819460295 ms
25.0149111130 ms
26.6491970348 ms
24.1136792525 ms
24.7863904491 ms
26.5977938537 ms
24.3670634117 ms
25.1557111309 ms
26.6821621184 ms
24.7327523470 ms
24.5000412064 ms
26.6033811560 ms
24.7665555259 ms
24.7307967912 ms
26.5980732188 ms
24.4743396158 ms