Skip to main content
GameDev.net gamedev.net
🔒 Locked

static variables

Started by Dwiel Apr 21, 2002 at 3:42 PM 4 replies 200+ views
Original Post
Dwiel
Dwiel
If i have a function that i run many many times, like every game loop, should i generally use static variables? (if they dont cause any problems with the code obviosly ) Thanx alot Zach Dwiel Tazzel3d
Dean Harding
Dean Harding
Simaple answer: no.

First off, once every game loop is not that often. 1,000 times a game loop is probably worth your scrutiny, but only once and you''re not even going to notice it.

Second of all, creating variables on the stack takes a really small amount of time (i.e. it''s just a matter of moving the stack pointer) and it doesn''t take any longer the more variables you add...

And lastly, unless you can prove by profiling your code that this is your bottleneck, then you''re trying to optimize the wrong thing. I can''t stress this enough, but always make sure you''re profiling the right thing. Don''t forget the golden rule: 90% or time is spent executing 10% of your code. There''s no point optimizing code that''s only being called 10% of the time!


codeka.com - Just click it.
Dwiel
Dwiel
Thanx for the help and quick response!!! I just figured i might as well do this as I was writting my functions even if it helped only a miniscule amount... its not like my tetris clone needs any more fps anyway, i am just trying to make it as fast as possible as i write it so that when i write harder stuff ill be more practiced

thanx again!

P.S. I assume that even though my tetris clone runs at 700fps, and the code is run anywhere from 1 to 5 times a cycle... that prolly still wont make a differecnce right?

thanx!
thuned
thuned
would the speed matter if i''m not using primitive types? what if i''m using a big class?
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
jaxson
jaxson
quote:
Original post by thuned
would the speed matter if i''m not using primitive types? what if i''m using a big class?


It depends on what your constructor is doing for your class. Of course, you don''t do anything of significance in the constructor, right?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.