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

Performance question: static variables

Started by dali Sep 13, 2006 at 8:08 AM 12 replies 1.4k views
Original Post
dali
dali
Hi, In a methode used very frequently i use static variables which brings in fact a lot of speed. I have not seen that in code by others so is this a good idea? I dont use multitasking so there are no critical situations either.. Thanks a lot
Gaudeamus igitur, iuvenes dum sumus!
joanusdmentia
joanusdmentia
Do NOT use static variable simply because you *think* they're faster. Use static variables if the DESIGN requires it.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
dali
dali
Thanks for the reply. But the speed seems to be 1/3 faster with static variables it also would be logical cause the variable has to be created only once. So why exactly should i dont use them?
Gaudeamus igitur, iuvenes dum sumus!
Jaywalk
Jaywalk
I gather that by "static variables" you're referring to static local variables (as opposed to static globals or static class variables).

Static local variables are basically global variables, except that they are not added to the global namespace, and as such can be seen and used in only one function (i.e. the function in which they are defined).

Think of what you're using the variable for. Would anything go wrong if you used a global variable instead of a local one? Aside from namespace collisions, the exact same things will go wrong with static locals.

There's nothing wrong with using static variables. Just understand what they are: global variables that can't be seen outside the function. That is what they are, and what they are for. They do not neccesarily give you extra speed unless your CPU's stack-relative addressing is significantly slower than absolute addressing (the timings might be the same, might not, may vary from one CPU to another, I don't know). From what you describe, it sounds like you're getting extra speed from using an initialised static in place of an initialised local, since statics are initialised only once. Unless you know what you're doing, just putting "static" in front of local variables is basically the same as using uninitialised variables.
Emmanuel Deloget
Emmanuel Deloget
Quote:
Original post by dali
Thanks for the reply. But the speed seems to be 1/3 faster with static variables it also would be logical cause the variable has to be created only once. So why exactly should i dont use them?


On-stack variables are not "created", unless they are of a non-POD type. In this case then yes, a speedup can be achieved. However, this speedup is very artificial as you might need to assign a value to the static variable - something which is, in most cases, slightly equivalent to calling the constructor and initialize the values.

I'd also like to see the code - in order to have a better understanding of the phenomenon, and maybe to give you hints about how you can achieve a similar result by keeping a more correct design.

Regards,
MaulingMonkey
MaulingMonkey
Static makes sense for expensive to construct objects. Typically in such a situation the code is written in such a way that not only that function has access to it, however - it's written as part of a larger system as a (shudder) singleton, or member of a manager class, etc., where the static keyword isn't what's used to control instantiation.

On the other hand, static is typically a bad idea for cheap to construct objects like PoD types, since commiting them to memory (so the same value can be reliably read next call, as is required with statics) is typically far more expensive when many variables can simply fit into registers.
dali
dali
In all methodes i gain a little speed using static variables.. Dont know would like to know if this is only my computer :) Look at the testcode bellow time

needed with static: 12011ms
needed without static: 13889ms

#include <iostream>using namespace std;#include <windows.h>#include <Mmsystem.h>void foo(){	int var = 15;	int var2 = var*15;	int var3 = var2*15;	int var4 = var3/15;	int var5 = var4/15;}void bar(){	static int var;	static int var2;	static int var3;	static int var4;	static int var5;	var = 15;	var2 = var*15;	var3 = var2*15;	var4 = var3/15;	var5 = var4/15;}int main(){	int loops = 99999999;	int timer;	timer=timeGetTime();	for(int i=0; i<loops; i++)	{		bar();	}	cout<<"needed with static: " << timeGetTime()-timer<<endl;	timer=timeGetTime();	for(int i=0; i<loops; i++)	{		foo();	}	cout<<"needed without static: " << timeGetTime()-timer<<endl;	char key;	cin >> key;	return 0;}


Gaudeamus igitur, iuvenes dum sumus!
DrEvil
DrEvil
You need to be testing release builds, and with a test that does some real work. In MSVC 2005 in release mode it compiles to 0 time for both. In debug its static: 10822 without static: 11853

Debug profiling is 100% worthless.
Conner McCloud
Conner McCloud
You can't derive anything from that test, its completely artificial.

I'm also going to guess that you're doing this test in debug mode, because in a release build both functions should have collapsed down into a nop and gotten optimized away.

CM
dali
dali
Jeah its correct that i am in debug mode and that this would make such a big difference... Hmmm did not know that in debug mode the compiler does not optimize. Thanks
Gaudeamus igitur, iuvenes dum sumus!
joanusdmentia
joanusdmentia
Allocating POD types on the stack takes a single addition, regardless of whether you have 1 local variable or 1000. As has been said profiling debug mode is useless, futhermore if you turn optimizations on the code you're trying to profile will be completely optimized out (ie. it won't exist). Why? Because it doesn't actually do anything. Both functions have no external effects so the function will end up empty, so the compiler won't even bother calling that function (unless you've explicitly disabled inlining), and it will then eliminate the empty loops.

Furthermore, if you were to do this for every function in your program you'd be increasing your memory requirements because every variable ever used would have to always exist, rather than only existing on the stack during the execution of each function.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o

Topic Locked

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

Sign in to reply to this topic.