FOR/NEXT or WHILE?

BlitzMax Forums/BlitzMax Programming/FOR/NEXT or WHILE?

DH(Posted 2005) [#1]
Writing a compiler (for scripts) and have found that FOR/NEXT loops use more (mathematically) then while/repeat loops...

Just a simple:
for x = 1 to 1000
next


Works slower than a

x=1
while x <= 1000
x=x+1
wend


Now this is in my scripting laguage where I convert everything to a pcode after tokenizing it all. Then merely execute the (x86-like) pcode.

I have tried to opimize it the best I can however keep coming to the same thing (that for/next loops are slower because they require slighly more math and line jumps).

Is this the same for most languages as well (IE blitz, VB, etc)?

Because in that case a slight optimization would be to change large loops (like updating verticies where the loop count can reach thousands) to a while/repeat rather than a for/next in some of the code I have used in the past.

any thoughts?


Dreamora(Posted 2005) [#2]
can't tell the same

for me local is faster (36to 63in averange)



the funny thing: without strict it is 4 times as fast as with strict??????


tonyg(Posted 2005) [#3]
While/wend is quicket on my system but turn debug off and the difference is a lot smaller.
I get the same slower rate with strict unless debug is turned off.


Dreamora(Posted 2005) [#4]
ah your right in debug off both are the same speed again (12ms with strict) ... *note to my list: always test with and without debug due to strange implementation parts of BM*


rdodson41(Posted 2005) [#5]
Debug off:
With or without Strict they are both hovering around 30.

Debug on:
With Strict both took around 1150 or so. Very strange.
Without Strict The ForNext loop took only 90 while the While loop took about 350.

Weird results...


Chris C(Posted 2005) [#6]
with strict I should think local vars not global will be faster...