Does it matter if you have a local declaration in a loop?

Started by monkeybot, January 04, 2014, 10:22:52 AM

Previous topic - Next topic

monkeybot

i was wondering if it matters,time wise,if you have a local declaration in a loop?
so i knocked this up the second loops always seems to complete slower than the first by 200>300 ms so is it in fact faster to declare vars in a loop?(seems unlikely!)


repeat
t=timer()
for q=0 to 10000
local a=1
local b=1
local c=1
local d=1
cls
print a+b+c+d+q
sync
;r=a+b+c+d+q
next
t1=timer()-t


t=timer()
for q=0 to 10000
cls
print a+b+c+d+q
sync
;r=a+b+c+d+q
next
t2=timer()-t
cls
print t1
print t2
sync
waitkey
until false


kevin

 
Quoteif you have a local declaration in a loop?

 Nope.

 These produce exactly the same code,
PlayBASIC Code: [Select]
       local A=2
A=2




your example code isn't timing loops, it's timing syncs.   Every time you sync,  the VM process is released and winblows takes over again..  


PlayBASIC Code: [Select]
Maxtests=100000

repeat

cls
frames++

t=timer()
for q=0 to Maxtests
local a=1
local b=1
local c=1
local d=1
next
t1#+=timer()-t

t=timer()
for q=0 to Maxtests
d=1
e=1
f=1
g=1
next
t2#+=timer()-t
print t1#/frames
print t2#/frames

sync
until false