private var on class level vs declared in frequently called function?

46 views
Skip to first unread message

Mark

unread,
Sep 19, 2014, 5:24:58 AM9/19/14
to haxe...@googlegroups.com
Dearest Community :)

I'm writing a fun-app in Haxe & openFL and bumped into "ENTER_FRAME" event -and measuring time elapsed from last call-.
It looks something like this (in event handler function) :

currentTime = Lib.getTimer();
deltaTime
= currentTime - previousTime;
previousTime
= currentTime;

The question is, for a function called very frequently (once every frame) which one is better, which is "handled better by haxe" so to speak?
a) declare 'currentTime' and 'deltaTime' function-scope, so it gets destroyed/re-created(does it?) every frame
-or-
b) declare all three variables class-level (as private) so entering handler will just overwrite value stored in memory?


Can't really tell which one is better / optimal, especially not if it varies depending on scenario.


Thank you for your help in advance!

All the Best,
Mark

Benjamin Dubois

unread,
Sep 19, 2014, 8:10:56 AM9/19/14
to haxe...@googlegroups.com
Since allocation is supposed to be expensive I'll go for b).

Didn't timing a big loop on your two option give you a decent answer ?

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Mark

unread,
Sep 20, 2014, 7:33:25 AM9/20/14
to haxe...@googlegroups.com
yes, 'b' sounds OK but I don't know this for HaXe (and e.g. if depends on platform or not). As I don't know HaXe's code.

was thinking about testing the way you recommend but that could not give a good picture. might give an answer for a specific usecase but not a "general approach" - which I think would come handy to everyone (i.e. function-scope vars, statics or class-scope / class-scope statics)

Luca

unread,
Sep 20, 2014, 8:58:01 AM9/20/14
to haxe...@googlegroups.com
Value types like Int will never be allocated on the heap like objects are, there is absolutely no reason whatsoever to even consider b) here unless your goal is to obfuscate your code and make your objects fatter than they need to be.

Mark

unread,
Sep 20, 2014, 11:21:22 AM9/20/14
to haxe...@googlegroups.com
Indeed but they get allocated. memory allocation takes time. the question is around that in HaXe.

Nathan Hüsken

unread,
Sep 20, 2014, 12:00:45 PM9/20/14
to haxe...@googlegroups.com
They should get allocated on the stack when the function is entered (at least I think that is the way it should be). Since a stack increase happens anyway, there is no overhead.
--

Luca

unread,
Sep 20, 2014, 12:08:30 PM9/20/14
to haxe...@googlegroups.com
Whilst accessing non-local variables 'does' have an overhead, as does making objects fatter (more member variables)

Johann

unread,
Sep 20, 2014, 12:10:18 PM9/20/14
to haxe...@googlegroups.com


On Saturday, September 20, 2014 5:21:22 PM UTC+2, Mark wrote:
Indeed but they get allocated. memory allocation takes time. the question is around that in HaXe.


Generally, Haxe (not HaXe) inherits the performance characteristics from its targets, so when it comes to performance considerations, it all depends on the respective target & runtime implementation. 

In this case, however, all of them currently use stack allocation for local vars (with the exception of local variables captured in the environment of closures). The stack is always pre-allocated, an hence it does not take any time to allocate memory on the stack. None of Haxe's target platforms are special in that regard. 

Mark

unread,
Sep 20, 2014, 12:15:52 PM9/20/14
to haxe...@googlegroups.com
Thank you for all the answers!
Reply all
Reply to author
Forward
0 new messages