Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
self vs. global — Gideros Forum

self vs. global

totebototebo Member
edited December 2016 in General questions
Can someone (@sinistersoft!) explain why self is consistently slower than global in this code?
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()
My Gideros games: www.totebo.com

Comments

  • self variables have to be looked up in a table belonging to whatever self is. Globals are more direct.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • n1cken1cke Maintainer
    Accepted Answer
    Global: 0.48127102760191
    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`.
  • Thanks guys. Yeah, sorry, I meant global vars are consistently slower than self. Thanks for the explanation. I'm going to stick with local vars whenever I can, blimey!

    Likes: SinisterSoft

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • Hmm - I mean local globals sorry....
    Main = Core.class()
     
    ITERATIONS = 5000000
    TEST_VAR @ 4353
     
    local global_var = TEST_VAR
    now it should be faster.

    Likes: antix

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • Yeah, I'll do that whenever I can. But I can't have the entire game in one scope (can I?!), so in some cases this isn't possible. So I was wondering whether to use self.var or global var, and I think the difference is quite small, but in favour of self.var.
    My Gideros games: www.totebo.com
  • SinisterSoftSinisterSoft Maintainer
    edited December 2016
    Use macro constants wherever you can, then the most commonly executed vars that need to be global, use local globals.

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • Bingo. This is now my mantra.

    Likes: SinisterSoft

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.