If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

INCR speed test

I found the INCR keyword just a few minutes ago, so I decided to give it a speed test. I used a speed test that is in the same section of this forum. I believe that the master himself wrote it. (Eros, that is!) My programming skills are limited, so I usually have to adapt what other people write.

Anyway, getting back on target. The following test takes about 31 or 32 msec to perform. After running it several times, the test only took 15 or 16 msecs. (I don't know the reason for the boost of speed.)

[code=thinbasic]'---
' Sample script to demonstrate how fast INCR is
' The below function is resolved MaxCount times
' This script was adapted from another script (almost exactly like this one)
'---

I wanted to compare INCR with the usual way I've incremented in the past. (X=X+1) Just changing the one line (as shown below) made the test take several msecs longer. This test usually ran around 47 msecs, though a few times it did come in at 32 msecs (I guess for the same reason INCR was faster sometimes).

[code=thinbasic]'---Loops and get result
FOR counter = 1 TO MaxCount
x=x+1
NEXT
[/code]

Re: INCR speed test

Mark,

when timing is so little, measuring it in multi threading environment (like Windows) can give very different results. I'm sure in your PC there are at least other 20/30 (if not more) process running at the same time. Everyone of those other process can do something (we do not know details) and when they do something they consume CPU cycles delaying other process (even if for few milli or micro seconds). The ideal situation would be to have just one process running (like old DOS) but now day this is not possible in standard PC. So to have credible results, it would be better to have tests that last at least few seconds instead of few milliseconds.

That said, back to the question: why INCR <variable> is faster than <variable> = <variable> + 1
thinBasic is an interpreted language so even few interpreting steps can make a lot of difference.

When you use INCR ... thinBasic just needs to make 2 steps: INCR followed by a variable. After that thinBasic has all the info to perform the operation.

When you use <variable> = <variable> + 1 thinBasic has to make a lot of parsing steps. Just to summarize a few info: first it identify a variable, than it assume it is an assignment so it will parse for equal sign, than it will start a recursive descendant parsing for a numeric expression, that it will assign the result to the assign variable ...

As you can see the two methods perform completely different.

To add 1 (or other values) to a variable, thinBasic also has <variable> += 1