PDA

View Full Version : INCR speed test



marcuslee
02-09-2008, 23:37
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.)


'---
' 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)
'---

'---Some predefined values
DIM n_str AS EXT VALUE 100
DIM n_lvl AS EXT VALUE 100
DIM n_wp AS EXT VALUE 100
DIM n_vit AS EXT VALUE 100
DIM n_adef AS EXT VALUE 100
DIM MaxCount AS LONG value 100000
dim x as integer value 1

'--Results and counters
DIM result AS EXT
DIM counter AS LONG

'---For time measuring
DIM T1 AS quad
DIM T2 AS quad

'---Used for string output
DIM Message AS STRING

'---Here we go ...
'---Ask if ...
Message = "This program will time increasing the value of x " & MaxCount & " times.\n"
Message += "Please press Yes to go on, NO to Stop\n"
DIM lResult AS LONG = MSGBOX(0, Message, %MB_YESNO, "Continue?")
IF lResult <> %IDYES THEN
STOP
END IF

'---Get initial time
T1 = gettickcount

'---Loops and get result
FOR counter = 1 TO MaxCount
incr x
NEXT

'---Get final time
T2 = gettickcount

'---Show results
MSGBOX 0, "mSecs:" & $tab & FORMAT$(T2 - T1, "#0") & $CRLF & _
"Result:" & $tab & result & $crlf & _
""


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).


'---Loops and get result
FOR counter = 1 TO MaxCount
x=x+1
NEXT


Why is INCR faster than X=X+1?

I'm sure there is some technobabble to explain this.

Mark

ErosOlmi
03-09-2008, 00:00
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

Ciao
Eros

marcuslee
03-09-2008, 01:09
Well, there sure is a lot that goes into just one line of code. And, I completely understand the multiprocess explanation. Thank you.

Just a side note: x+=x performs just like INCR x ... in terms of speed, at least on my machine.

Mark (or marcus or marcuslee ... I answer to them all!) :P