Variable declaration affects timing, reasonable?

enonod

Well-Known Member
Licensed User
Longtime User
I declare two Int variables as Process Global and the code runs some 70ms faster per cycle than if I declare locally. That is some 9-10%
It is consistent and the only change I make between tests.
Does this seem reasonable?
 

enonod

Well-Known Member
Licensed User
Longtime User
I don't know why but would have expected local variables to be faster, but on the other hand the declarations do in fact get repeated as you point out.

Thanks for the confirmation.
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
This is not an issue, I was simply curious.
Cannot post it at the present but the time is checked in this main timer loop which is set at 100ms though it should not affect it and has been set at other figures below 1sec.

B4X:
Sub Process Globals
Dim dX, dY As Int
...
End Sub

Sub Timer1_Tick
Dim in As Double
in= DateTime.now
   Draw
   Activity.Invalidate
   Log(DateTime.Now - in)
   ft=False
End Sub

Sub Draw
Dim dX, dY As Int
...
End Sub
The only change between tests is to turn on or off the declaration in each sub.
The difference is typically (because it varies slightly due to Android interrupts presumably) from 750/770ms with Global and 825/860ms Local consistent.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

Maybe using locals instead of globals creates more work for the Android garbage colllector?

That'd imply that there is little if any performance difference in the code regardless of whether locals or globals are used - but the extra work performed by the garbage collector could be significant?

Martin.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Maybe using locals instead of globals creates more work for the Android garbage colllector?
Nope, as Ints are primitives they are not subject to garbage collection.

Global variables are static and only initialised to zero once when the program starts. Locals are allocated on the stack and initialised to zero every time the Sub is called. I don't however see that initialisation causing the 70mS difference.

You don't show how you are using those variables, presumably heavily and in a loop though even then a 10% difference in execution time is a bit puzzling. If you are using a version of Android, 2.2 or later, which uses a Just In Time compiler at runtime to increase performance what you may be seeing is some optimisation that the JITter is doing. If it's an earlier version of Android, which interprets the byte codes at runtime with the Dalvik Virtual Machine, maybe the interpreter can access static variables more efficiently.
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thank you for the insight to variable setup.

The variables are indeed used heavily inside a double loop. The loops are servicing arrays and drawing on a canvas ready for display.

What I neglected to mention and is only now becoming apparent may be significant, (and creating a mountain out of a molehill) is that I am using the emulator.
I apologise if that makes it more obvious regarding the quantity of mSecs.
I was only curious, but that is how one learns I believe.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…