![]() |
Is there a better way?
I have a curiosty I wanted to shed light on. I was curious if there's any point, or if it's worth it, to setup variables like so:
"Normal" method Lua Code:
What about making a new [local] table to house frame elements? Lua Code:
I guess what I'm really asking is; is there extra ways to optmize variables? Also, does the length of variable names (ie. "addon.MainFrame.MyLabel.Text" vs "main.MyLblText") affect overall addon performance? |
According to http://www.lua.org/gems/sample.pdf locals are faster than globals. Thus the second version should be faster.
Is it worth it? Depends on what you're doing. Probably not. Regarding your second question: as every table lookup needs time I would guess that nested tables are slower. |
Thanks for the info and link!
|
I do something like this sometimes:
Lua Code:
Lua Code:
|
Quote:
Lua Code:
|
Thanks guys, this really helps :)
|
Had another quick question with this code:
Lua Code:
Is "var" in File B's function considered global, or local? EDIT: Based on what I've found via google, it seems they are local (which I had figured). If this is incorrect, please say so :) |
Yeah, it is local to the function, in exactly the same way as it would be had you done this:
Code:
function addon.FunctionInFileB() |
Quote:
|
So based on what I've leared from all this, I would assume Method A is better than Method B?
Method A Lua Code:
Method B Lua Code:
|
Technically yes, but when you're going through your init process, CPU power spent isn't as critical. The only time passing a value to a local makes a real difference is in code that is meant to run often. Examples of these are OnUpdate scripts and CombatLog events.
|
Still good habits to get into, nonetheless.
|
I'm just imagining how cluttered the local namespace would get in really big projects if everyone started doing this with init code. I honestly don't want to be the one to debug code like that.
Occasionally, if I don't need a reference back to the UI object I'm creating, I do something like this. Note: This is only an example. Lua Code:
The do...end block forces a lexical scope to be defined inside it. After the block is done executing, all locals defined inside it are freed. |
And if this
Lua Code:
|
All times are GMT -6. The time now is 03:16 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI