Stupid variable incrementation speed question!

BlitzMax Forums/BlitzMax Beginners Area/Stupid variable incrementation speed question!

BlitzProg(Posted 2010) [#1]
Because i went from blitzbasic to blitzplus and may now code in blitzplus and blitzmax, i write in blitzmax the following

x=x+1


But since Blitzmax support the following
x=+1

is the incrementation faster? (and how much faster if there is actually a big difference?)

(Because the first exemple is more like a work around with the variable calling itself to make an addition, so...!)

Thanks if you can answer! :)


Jesse(Posted 2010) [#2]
it should b x :+ 1 not x =+ 1
I have never really tested it but I am sure you can figure it out by doing some tests(good beginner exercise). I am sure it's not worth loosing sleep over it.


Czar Flavius(Posted 2010) [#3]
I am pretty sure they work both the same in the computer. I made a little test for you
Strict

Const test_runs = 10000000

Local n = 0
Local start_time = MilliSecs()
For Local i = 0 Until test_runs
	n :+ 1
Next
Print "n :+ 1 took " + String(MilliSecs() - start_time) + " milliseconds"

n = 0
start_time = MilliSecs()
For Local i = 0 Until test_runs
	n = n + 1
Next
Print "n = n + 1 took " + String(MilliSecs() - start_time) + " milliseconds"


n :+ 1 took 295 milliseconds
n = n + 1 took 294 milliseconds


The difference of 1 is probably just due to chance and doesn't mean anything. They both go at the same speed, so use whichever you like best!


Matty(Posted 2010) [#4]
As has probably been said many times before, in other threads on similar questions, these types of 'optimisations' are pretty much worthless as these instructions are not going to be the bottle neck in your code.


BlitzProg(Posted 2010) [#5]
Alright, thanks you very much! =)


ImaginaryHuman(Posted 2010) [#6]
Taking a guess here. ... in assembler, x=x+1 would:

a) read x into a variable
b) read 1 and add it to the variable
c) copy the result to the x variable

whereas x:+1 would

a) read 1 and add it to the x variable

So sounds potentially faster, but the memory access to read the variable and stuff probably negates any gain.


Shortwind(Posted 2010) [#7]
There is no difference in how the compiler builds either version. If you look at the produced ASM code you'll see that both commands are "set-up" pretty much the same way.

The only time I can find any difference is in the case of Local versus Global variables. If "x" is a local variable, and a "Int" (for example) the value is stored in the stack and all additions are done in the registers. Where as with the Global variables the ASM code must do a indirect add using the variables memory address.

IE: add eax,1 - for example of a local variable.

IE: add dword [_bb_x],1 - for example of a global variable.

The only difference you really need to concern yourself with is which version you find easier to read.


BlitzProg(Posted 2010) [#8]
ImaginaryHuman > This is the reason I was actually asking :P

Shortwind > I guess i'll stick with x=x+1 because of blitzplus, then. :D


Ghost Dancer(Posted 2010) [#9]
Another consideration - if you use long variable names (like I do), x:+ 1 makes your code much easier to read IMO. For example,

myType.subType.aReallyLongVariableName:+ 1

looks much neater than

myType.subType.aReallyLongVariableName = myType.subType.aReallyLongVariableName + 1

and prevents any unnecessary scrolling in your editor, esp if it is indented too :)