It looks like you're new here. If you want to get involved, click one of these buttons!
Main = Core.class() ITERATIONS = 5000000 TEST_VAR @ 4353 global_var = TEST_VAR function Main:init() self.self_var = TEST_VAR local t = os.timer() for i=1, ITERATIONS do global_var = global_var + global_var/global_var end print( "Global:", os.timer() - t ) local t = os.timer() for i=1, ITERATIONS do self.self_var = self.self_var + self.self_var/self.self_var end print( "Self:", os.timer() - t ) local var_local = TEST_VAR local t = os.timer() for i=1, ITERATIONS do var_local = var_local + var_local/var_local end print( "Local:", os.timer() - t ) end Main.new() |
Comments
https://deluxepixel.com
Self: 0.47692719142651
Local: 0.07365396944806
Not on my PC :-)
You compare table access `self.self_var` with global variable `global_var` access time. `self` is just implicit parameter, it's speed is the same as any other local variable/parameter. Lua searches like this:
1) local variable/parameter
2) upvalue
3) global variable
Upvalues and global variables are implicitly stored in tables (`debug` library can access upvalues, `_G` table is for global variables). Therefore global variable access speed for `_G` is similar to usual table access for`self`.
Likes: SinisterSoft
Likes: antix
https://deluxepixel.com
https://deluxepixel.com
Likes: SinisterSoft