Page 1 of 3
for who using for,while , this show you how it's working
Posted: Wed Sep 21, 2011 10:59 am
by freepurebasic
The difference between for and while in any programming language :
Code: Select all
howmannytimes=10000
;check if timeGetTime_() it's work
startscript = timeGetTime_()
stopscript = timeGetTime_()
wasnothing= stopscript-startscript
startscript = timeGetTime_()
Delay(howmannytimes)
stopscript = timeGetTime_()
washowmannytimesmilisecconds= stopscript-startscript
;^----------------i was ckecking if timeGetTime_() was work
;you must get 0 and howmannytimes (min and max)
startscript = timeGetTime_()
For x=1 To howmannytimes;wait "howmannytimes" milisecconds
Delay(1);this is 1 milliseccond KNOWN TIME
Next
stopscript = timeGetTime_()
wasfor= stopscript-startscript
startscript = timeGetTime_()
k=0
While(k<howmannytimes) ;wait "howmannytimes" milisecconds
Delay(1);this is 1 milliseccond KNOWN TIME
k+1
Wend
stopscript = timeGetTime_()
waswhile=stopscript-startscript
Debug "Results:"
Debug "void execution:" + Str(wasnothing)
Debug "Delay(howmannytimes) proc's execution:" + Str(washowmannytimesmilisecconds)
Debug "for execution:" + Str(wasfor)
Debug "while execution:" + Str(waswhile)
the same results in php or any script interpreter/pre-compiler / compiler
if you don't believe just TRY YOURSELF
Re: for who using for,while , this show you how it's working
Posted: Wed Sep 21, 2011 12:05 pm
by c4s
freepurebasic wrote:Code: Select all
Debug "Results:"
Debug "void execution:" + Str(wasnothing)
Debug "Delay(howmannytimes) proc's execution:" + Str(washowmannytimesmilisecconds)
Debug "for execution:" + Str(wasfor)
Debug "while execution:" + Str(waswhile)
First Rule For Performance Tests Using PureBasic:
Disable the debugger!
Re: for who using for,while , this show you how it's working
Posted: Wed Sep 21, 2011 1:02 pm
by Ramihyn_
You want to learn about resolution/precision of Delay() and TimeGetTime().
Re: for who using for,while , this show you how it's working
Posted: Wed Sep 21, 2011 1:11 pm
by freepurebasic
not really wasn't my intention to do this ... you wrong understand,
the facts will be right this : you must get a number bigger for "for 's execution" than "while execution" wich is great !!!
questo erat demonstrandum
be sure you have buyed purebasic , here i have not so i don't know if work similary to freebasic (freebasic.net)
is no need to disable debugger because the script is relative with anyone language of this fk world .. so modify it for c++ and test it ,on windows api , you will get the same results ..

Re: for who using for,while , this show you how it's working
Posted: Wed Sep 21, 2011 1:43 pm
by Ramihyn_
I dont understand half of what you say, but your measuring is totally bogus like that. Let's ignore the whole "loop" for a moment and do the following:
Code: Select all
FileID.i = CreateFile(#PB_Any, "delayunrolled.pb")
For f.i = 1 To 10000
WriteStringN(FileID, "Delay(1)")
Next
CloseFile(FileID)
Run the file, load the delayunrolled.pb, add your time measuring like at the top:
and at the bottom
Code: Select all
stopscript = timeGetTime_()
tdiff = stopscript-startscript
MessageRequester("Blah...", "unrolled : " + Str(tdiff))
Compile it as an executable and run it. Run it again, run it while your machine is heavily in use, run it again. Compare the results, do the same with a small number of repetitions like 50, do it with a large number like 30000. Compare the numbers - what did you expect to get and what did you get?
Re: for who using for,while , this show you how it's working
Posted: Wed Sep 21, 2011 2:03 pm
by breeze4me
Case 1: Debugger off.
Results:
void execution:0
Delay(howmannytimes) proc's execution:10000
for execution:19531
while execution:19531
Case 2: Debugger on.
Results:
void execution:0
Delay(howmannytimes) proc's execution:10000
for execution:19531
while execution:19532
the facts will be right this : you must get a number bigger for "for 's execution" than "while execution" wich is great !!!
no differences.
Re: for who using for,while , this show you how it's working
Posted: Wed Sep 21, 2011 2:37 pm
by luis
Delay(1) is not guaranteed to give 1 ms delay.
Anyway, what this code should demonstrate ?
Especially considering you are putting a considerable delay inside the loops and so actually leveling the for/next and while/wend performances ?
BOH. I don't get it.
Moreover, where is the question ?
Re: for who using for,while , this show you how it's working
Posted: Wed Sep 21, 2011 2:39 pm
by Thorium
I dont get what you want to tell us.

Re: for who using for,while , this show you how it's working
Posted: Wed Sep 21, 2011 3:02 pm
by freepurebasic
there are people(MAYBE not you,not sure) that make confusion between for and while , so was no question was a demo that for is slow and while is speeding up.
giving this example i said : this is equal for any "language" (script interpreter/pre compiler/compiler) the "for" is slower than "while".
just all
i'll prepare you for php too if you want to see it (winbinder) ,works in win 7 too ,better .
this is php cgi solution ,so you can load a library and access a function as well ,in PHP !
and how i said before : if you adapt this code for any language you will have the prove the while sentence writed so large is faster like execution than a for sentence.
that's all puppies !

Re: for who using for,while , this show you how it's working
Posted: Wed Sep 21, 2011 3:16 pm
by freepurebasic
breeze4me thank you man !!!!!!!!!!!!!!!
thank you !!!!!
Code: Select all
for execution:19531
while execution:19532
try to bigger processors (over dual core) you will get other data
yes!
now try it for 3000 000 times you will understand it
then try to modify a script with graphics ,3d too , i sow some examples here, try to convert all for cycles to while cycles and see the difference using your : Windows Task Manager / performance (xp,vista,7)
or Windows Task Manager / performance .. resource monitor
and see your processor when running a "for" based script and then see "while" based (the same graphic script)
Re: for who using for,while , this show you how it's working
Posted: Wed Sep 21, 2011 3:32 pm
by luis
So you are saying, as a general rule, not knowing how any language can specifically implement a certain construct, that a "for/next" is always slower then a "while/wend" ?
I don't see why.
And specifically:
Code: Select all
For k = 1 To 3
Debug k
Next
; For k = 1 To 3
MOV dword [v_k],1
_For1:
MOV eax,3
CMP eax,dword [v_k]
JL _Next2
; Debug k
; Next
_NextContinue2:
INC dword [v_k]
JNO _For1
_Next2:
k = 1
While k <= 3
Debug k
k + 1
Wend
; k = 1
MOV dword [v_k],1
; While k <= 3
_While3:
MOV ebx,dword [v_k]
CMP ebx,3
JG _Wend3
; Debug k
; k + 1
INC dword [v_k]
; Wend
JMP _While3
_Wend3:
I disagree, and I don't know where you took this strange notion from.
Your test on my PC
Code: Select all
[16:35:36] Results:
[16:35:36] void execution:0
[16:35:36] Delay(howmannytimes) proc's execution:10000
[16:35:36] for execution:10001
[16:35:36] while execution:10000
Anyway, the test is meaningless because as I said you leveled the timing of the two loops by putting a delay inside. So the time spent in the loop code become irrelevant.
Re: for who using for,while , this show you how it's working
Posted: Wed Sep 21, 2011 4:41 pm
by freepurebasic
nice but is wrong .... you are wrong.
can you did it again ?
anyway nice asm solution but isn't close
Code: Select all
I said you leveled the timing of the two loops by putting a delay inside.
=
Code: Select all
what you are seeing there :delay(1) is one miliseccond known time so 10000 times for 1 is 10000 known time is that you need to see to know the api function really works in the compiler you choose to calibrate the script,to verify if method choosen can deliver the answer you need using the same line of the method.
so having 0 and max-of-loops * 1 milliseccond of delay can you mesure using the same method the speed of the script/the program execution (not only in purebasic but in php,mysql,javascript,c++,pascal,fortran,freebasic,dev c,bloodshed c,etc etc etc etc)
using the same method
your asm script secvences are two : one for "for" and the seccond for "while"
but do you calculate the processor ticks ?????
i see a script but no answer and no calculation there
and depence where you tested : in a virtual machine or a processor on windows ???
uh oh uh oh

Re: for who using for,while , this show you how it's working
Posted: Wed Sep 21, 2011 5:42 pm
by Thorium
freepurebasic wrote:nice but is wrong .... you are wrong.
can you did it again ?
You dont know what you are talking about.
Fix your time messuring first.
Re: for who using for,while , this show you how it's working
Posted: Thu Sep 22, 2011 9:21 am
by freepurebasic
Thorium with his hammer ...anyway you dance very well
Continue like this ...
Fix your time messuring first.
hmmm ... is already calculated you need JUST to look CAREFULLY the results on screen to make decisions.....
Delay(howmannytimes) it's the measure !!!
..api windows / purebasic measure i don't interesting if Delay works really , what i interest is- using Delay in all cases like measure to obtain the same result: "While cycle" is speeding up , "for cycle" is slower than "while" ! this i need to know 100% to make a decision.
so my advice is : use "while" instread "for" in ANY language!
Re: for who using for,while , this show you how it's working
Posted: Thu Sep 22, 2011 9:58 am
by breeze4me
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