Thanks Rob,
Is there any good profiler for Corona apps so I can measure how fast my various code sections are to prevent premature optimization? [import]uid: 206803 topic_id: 36688 reply_id: 145047[/import]
Thanks Rob,
Is there any good profiler for Corona apps so I can measure how fast my various code sections are to prevent premature optimization? [import]uid: 206803 topic_id: 36688 reply_id: 145047[/import]
As a rule of thumb I say don’t worry about globals, unless you use then in a for-loop or big conditional branch - in which case you need to localise them first and then use this local variable in your code. Either way, there is no reason to be as afraid of globals as is often advertised here.
In places where you need to get or set a variable once every while, don’t worry about it, and else make 'em local.
Don’t forget that when you say something like local newTable = myGlobalTable
the table newTable is not a copy but a direct reference to the table myGlobalTable. In other words, once you do this in a block of code, you can use the local table newTable in your code, and all changes will actually (also) be done to the original global table myGlobalTable, without the speed penalty of using the global table - because you’ve looked up the “address”, so to speak, of this global table, so you know where to work and things can go a lot faster. [import]uid: 70134 topic_id: 36688 reply_id: 145056[/import]
I remember one of the performance threads going into millisecond details on one method versus another, but the gist is that local is much faster, like the difference between 10 and 100. Performance wise it makes a lot of sense to localize a global when possible under those conditions.
For “here and there” use they are absolutely fine though. I use a handful of them on most projects for true globals - things that I basically want to know everywhere and rarely (if ever) need to change them, but also as work arounds for things like scene params.
tl;dr try to localize if you’re going to use in enterFrame, but it’s fine for most other uses. [import]uid: 41884 topic_id: 36688 reply_id: 144985[/import]
Thanks Richard9,
I’m scared to use them but I think there are scenarios that you need to use them or else they would remove it from Lua altogether.
Also this emphasize, if it’s truly this bad as people say, can be done in other languages because they have same concept but I haven’t seen such thing.
It can be due to the nature of implementation of globals in Lua but I really would like to read about them in detail so I can know when to use them and can do a clear calculation based on, at least some, facts rather than skipping them blindly. [import]uid: 206803 topic_id: 36688 reply_id: 144990[/import]
I wouldn’t be scared to use globals. I would just try to take advantage of locals and use globals only when you feel they make sense. (Quite frankly a lot of Corona apps just aren’t using enough performance for it to matter.)
The “bad” about globals is that they are:
(a) not very visible (you can declare something in one file and overwrite it in another)
(b) difficult to garbage clean because they can exist anywhere. (it’s up to you to nil them)
© slower to reference (noticeable mainly in enterFrame)
There’s nothing wrong about any of those, per se, but the Lua gurus try to come up with localized solutions in order to minimize risk.
[import]uid: 41884 topic_id: 36688 reply_id: 144993[/import]
Thanks Richard9. [import]uid: 206803 topic_id: 36688 reply_id: 144997[/import]
It’s a matter of how you’re using them. Let’s say you decide to make your game’s score global. You initialize it to 0. You access it to display the score as text when you create the display.newText() object.
Then lets say you score points picking up coins every few seconds. Finally your game over screen reads the score to draw the final score as a new display.newText() object.
You have touched the variable 3 times + 2 access per score update (update the score, use it to update the on-screen text). The difference between it being global or local is negligible.
Now lets say you’re trying to render a Mandelbrot fractal and you have a global variable inside several nested loops and you’re calling non-localized math functions out of global space. This will kill your performance. When you are doing calculations as fast as the device will run, global v. local performance will be quite noticeable. [import]uid: 199310 topic_id: 36688 reply_id: 145001[/import]
Thanks Rob,
Is there any good profiler for Corona apps so I can measure how fast my various code sections are to prevent premature optimization? [import]uid: 206803 topic_id: 36688 reply_id: 145047[/import]
As a rule of thumb I say don’t worry about globals, unless you use then in a for-loop or big conditional branch - in which case you need to localise them first and then use this local variable in your code. Either way, there is no reason to be as afraid of globals as is often advertised here.
In places where you need to get or set a variable once every while, don’t worry about it, and else make 'em local.
Don’t forget that when you say something like local newTable = myGlobalTable
the table newTable is not a copy but a direct reference to the table myGlobalTable. In other words, once you do this in a block of code, you can use the local table newTable in your code, and all changes will actually (also) be done to the original global table myGlobalTable, without the speed penalty of using the global table - because you’ve looked up the “address”, so to speak, of this global table, so you know where to work and things can go a lot faster. [import]uid: 70134 topic_id: 36688 reply_id: 145056[/import]