E enonod Well-Known Member Licensed User Longtime User Jan 13, 2012 #1 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?
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?
thedesolatesoul Expert Licensed User Longtime User Jan 13, 2012 #2 It is possible especially if you are doing recursion or something complex. Upvote 0
E enonod Well-Known Member Licensed User Longtime User Jan 13, 2012 #3 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
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.
klaus Expert Licensed User Longtime User Jan 13, 2012 #4 Can you post your test project source code as a zip file. Would be intersting to test it. Best regards. Upvote 0
Can you post your test project source code as a zip file. Would be intersting to test it. Best regards.
E enonod Well-Known Member Licensed User Longtime User Jan 14, 2012 #5 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. Code: Copy 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
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. Code: Copy 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.
warwound Expert Licensed User Longtime User Jan 14, 2012 #6 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
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.
agraham Expert Licensed User Longtime User Jan 14, 2012 #7 warwound said: Maybe using locals instead of globals creates more work for the Android garbage colllector? Click to expand... 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
warwound said: Maybe using locals instead of globals creates more work for the Android garbage colllector? Click to expand... 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.
E enonod Well-Known Member Licensed User Longtime User Jan 14, 2012 #8 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
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.