XlegoXJoin Date: 2008-06-16 Post Count: 14955 |
Here's a short but usefull optimization. At the very start of your scripts, put this:
local game = game
How you may ask? Because in Lua, accessing global values takes about 10 times as long as accessing local ones, so if you make a local copy of game, all of the indexes of "game" your script makes will run 10 times faster.
Neat eh?
-xLEG0x
=== *WHY* it works, warning technical details beyone this point ===
The reason that accessing globals takes longer than accessing locals is that locals are stored as an *exact* position in your computer's memory, but globals are stored as an "identifier" to a global table.
When Lua wants to access a local, all it has to do is look at the adress in memory that the local should be stored at.
On the other hand, when it wants to access a global, it has to search through the table. In Lua, tables are stored as "next-last-key-value" packets. They have two pointers to the next and previous values in the table, what index the current packet is, and what value the current packet is storing.
So when Lua has to find a specific index in the table, it has to look at every other index until it gets to the one you want. That means the more stuff there is in the global environment, the more stuff has to be searched through for Lua to find the global you want.
So, basicaly, if you use an index more than once, you should declare it as a local varaible, and it *will* increase performance. |
|
|
NEAT... I'll keep this in mind.
Thanks! |
|
MeeloJoin Date: 2008-07-04 Post Count: 14763 |
Ever since he posted this on the other scripting help, I've become an optimization freak. |
|
AeacusJoin Date: 2007-12-20 Post Count: 1006 |
Awesome, I never knew that would help :D |
|
|
Ooh, very fancy. I think I'll have to implement that into my scripts. |
|
|
Uhh, repost? You did this before... in script help... |
|
earth100Join Date: 2007-12-13 Post Count: 2088 |
Would
local Workspace = Workspace
Work as well? |
|
XlegoXJoin Date: 2008-06-16 Post Count: 14955 |
Yea, this explaination is slightly better though, the last one was a bit sloppy, and it's better suited in this new forum. |
|
MicroUserJoin Date: 2009-03-29 Post Count: 3601 |
[ Content Deleted ] |
|
XlegoXJoin Date: 2008-06-16 Post Count: 14955 |
"Would
local Workspace = Workspace
Work as well?"
As I said at the end, *any* value you use more than 1 time would be worth it... |
|
MicroUserJoin Date: 2009-03-29 Post Count: 3601 |
[ Content Deleted ] |
|
dwighterzJoin Date: 2008-03-29 Post Count: 5057 |
Very good idea. I could never have thought of that. |
|
|
10 times faster you say? Dang.
That's so cool. |
|
Clone512Join Date: 2008-03-02 Post Count: 7810 |
So many people don't believe me when I say local variables aren't useless and they run faster, maybe now they'll believe me. |
|
|
Nice. I would have to keep this in mind, instead of leaving the top blank. |
|
sonic911Join Date: 2008-02-28 Post Count: 131 |
now i understand |
|
sdfgwTop 50 PosterJoin Date: 2009-01-08 Post Count: 41681 |
Bump phail?
Man, I always knew locals were accessed quicker, but I never thought of that :P |
|
sncplay42Join Date: 2008-11-27 Post Count: 11891 |
@sd: I didn't realize this was a bump at first, and then I saw level and thought, "What?" |
|
sdfgwTop 50 PosterJoin Date: 2009-01-08 Post Count: 41681 |
xD Me too. |
|
Person299Join Date: 2008-02-28 Post Count: 7952 |
Wow, good idea. |
|
sncplay42Join Date: 2008-11-27 Post Count: 11891 |
I often make functions local. |
|
TaslemGuyJoin Date: 2009-06-10 Post Count: 12174 |
hm... Never considered this. |
|
SnateraarJoin Date: 2008-04-15 Post Count: 3901 |
Grammar police!
Basicaly = wrong
basically = correct
>.<
And I never thought of this optimization... |
|
|
|