Quote: "Quote: "- SYNC before starting a new timing to give windows its chance early to take time away from you, making it less likely during the time-sensitive period."
I'll see if that makes a difference and report back."
Nope. The effect is the same.
Quote: "Quote: "For the ASM processing, you can use SWAP ARRAY ITEMS to swap items in an array instead of copying too."
I'll try that too. Thanks."
That made a BIG difference to that particular method! Even with strings this is now the best method (marginally) - but the weird effect on the other timings is still there. What's even odder is that the ASM method is also reported as faster when it's first. The effect seems unrelated to number of iterations as I said before.
Quote: "Quote: "- Use SET TIMER RESOLUTION to eliminate timing differences in windows, or use HITIMER with a resolution higher than 1000hz."
Is that an issue when measuring timer() differences of several hundred?"
That made no significant difference to the results when I used the following code.
set display mode desktop width(), desktop height(), 32
sync on : sync
dim colours(2) as string
colours(0) = "red"
colours(1) = "green"
colours(2) = "blue"
randomize timer() ` initialize random numbers to a random start
nSamples = 100000
sync
` KP's modulo method
KP2 = HiTimer(10000)
last_colour = rnd(2)
for i = 1 to nSamples
current_colour = (1 + last_colour + rnd(1)) mod 3
colour$ = colours(current_colour)
last_colour = current_colour
next i
KP2 = HiTimer(10000) - KP2
sync
` KP's rejection method
KP1 = HiTimer(10000)
for i = 1 to nSamples
` initialize the previous colour to something impossible
Last_Colour=-1
repeat
//current_colour is a variable
current_colour = rnd(2)
until current_colour<>Last_Colour
Last_Colour = current_colour
` lookup the colour so it could be used for something
colour$ = colours(current_colour)
next i
KP1 = HiTimer(10000) - KP1
sync
` DC's method
DC = HiTimer(10000)
last_colour = rnd(2)
for i = 1 to nSamples
current_colour = rnd(1)
if current_colour >= last_colour
current_colour = current_colour + 1
endif
last_colour = current_colour
colour$ = colours(current_colour)
next i
DC = HiTimer(10000) - DC
sync
` an inefficient string array swapping method
` **** for some odd reason, any method after this one seems to run faster WHY?
`ASM = timer()
ASM = HiTimer(10000)
` get a random start
colour = rnd(2)
swap$ = colours(colour)
colours(colour) = colours(2)
colours(2) = swap$
for i = 1 to nSamples
swap array items colours(), rnd(1), 2
colour$ = colours(2)
next i
`ASM = timer() - ASM
ASM = HiTimer(10000) - ASM
print "KP1 = ", KP1, " KP2 = ", KP2, " DC = ", DC, " ASM = ", ASM
sync
wait key
end
The effect is still there - and it's quite marked. Here are some typical results (using DBP timer()):
Method: KP1 KP2 DC ASM
ASM first: 325 302 294 291
ASM last: 555 549 517 457
Quote: "Not exactly, since such procedures may make other operations perform worse, while other procedures that are beneficial to other operations may make this operation perform worse. It's just a case of letting the CPU do what it can to optimize for it."
That does make it rather difficult for us mere users to optimise our programs - except for obvious things like not calculating something twice.
[Edited to include new code.]