Archived

This topic is now archived and is closed to further replies.

Landsknecht

Which is faster?

Recommended Posts

In the deepest nestings of multiple loops, there is a function call. And inside that function, there lives a variable. The guru on the mountain has not the knowledge... Is it faster to access a static variable that gets created once on the heap... Or to create the variable on the stack EVERY loop... Just wondering. Landsknecht

Share this post


Link to post
Share on other sites
quote:
Original post by AfTeRmAtH
in every loop, create the static variable. That puts it in the stack and keeps it in register(s).



Static variables don''t go on the stack. And you can''t guarantee it will stay in a register.

Share this post


Link to post
Share on other sites
Static variables go on the heap, right? At least that''s what I always thought. Its a moot point now anyway... I figured out a way to yank the poor variable away from its nice warm nest of loops and stick out into the wide world - thus avoiding the issue.

Issue avoidance is a high point in my life...
Landsknecht

Share this post


Link to post
Share on other sites
Not on the heap as newed variables are. I prefer to think of static vars as being allocated next to the code that creates them.

Share this post


Link to post
Share on other sites
static varibles are stored in memory, hence when leaving a function they keep there value and get only inited once (ie within the actually image of the exe file). varibles without the static keyword will be created on the stack, and remain there untill you leave the function. they DONT get reallocated every interartion through a loop.


  
void func1(void)
{ // i is allocated here every call

for(;;)
{
int i; // only allocated once during the length of the loop

}
} // i is no longer valid


// now i is allocated at exe load (ie within the data section

// of the exe). it is never allocated again

void func2(void)
{
for(;;)
{
static int i; // only allocated once

}
}


varibles are never actually allocated where you place them. they are either allocated at exe load (within the data section) or on the stack when entering a fucntion.

in the case of the loop, the compiler will see no difference whatsoever. since you dont allocate the varible each iterartion. however when calling a function you pass varibles which get pushed on the stack and the varibles in the function are also created on the stack at each call. though its quite silly to start ripping all your varibles to global for speed. you will eventually make your code so messy, it will actually get somewhat slower.

Share this post


Link to post
Share on other sites
Um, hey dude... You seem to think that memory and "the stack" are different things. At least that is what the phrasing of your post makes it sound like.

Also, I never said I pulled the variable to a global scope. I just found a better place for ioutside the loops. You might wanna read the posts a little closer next time.

Oh well,
Landsknecht

P.S. You are wrong... Using this code as a test proves it.

  
//With tmp as a static var...

void main()
{
for(int i = 0; i < 1000; i++)
{
static int tmp;
std::cout << tmp++ << std::endl;

}
}//Profile timing == 152.206


//Without static

void main()
{
for(int i = 0; i < 1000; i++)
{
int tmp;
std::cout << tmp++ << std::endl;

}
}//Profile timing == 279.240



Pretty significant difference, wouldn't you say?


[edited by - landsknecht on March 19, 2002 3:09:19 AM]

Share this post


Link to post
Share on other sites
while your showing differences that are pretty significent. you are not testing correctly. you are placing chance and giving static an advantage since it iwll more likly start at zero hence have smaller numbers to print (most compilers zero out static varibles that are not inited during declaration).

try using

  
void main()
{

for(int i = 0; i < 1000; i++)
{
int tmp=0;
printf("%d", tmp++);
}
}

// and


void main()
{

for(int i = 0; i < 1000; i++)
{
static int tmp;
tmp=0;
printf("%d", tmp++);
}
}


which you will notice will be very similar timings. though even this test is not very good since you are relying on io routines. i get about 0.01 ro 0.015 ms differences depending on which runs i take. and each run (of the same function) is on avg almost 0.015ms different from each other. so thing pretty much seem to depend on the io call, rather then the actually varible declaration.

also memory and the stack are quite different concpetually. granted they are both in RAM, the cpu and OS can place them in different places in RAM. while technically you can ocnsider it one and the same (ie all in memory) you must realize that static vraibles get placed in your data section while stack varibles get created at runtime (via moving the stack pointer). so they are different since static varibles are precreated as the exe is loaded into memory. in fact if you knew where in the data section a particular varible was located you could modify its start value using a hex editor. you CANNOT do this with a stack varible unless you start editing machine code in the code section.

and i thought you made it global because you said you yanked and "stick out into the wide world" which means to me at least that you made it accessible by the world hence made it global. possible you misunderstood my comments as they make more sense if you know assembly and basic to intermidate understanding of the workings of a computer at the hardware level.

Share this post


Link to post
Share on other sites
This is interesting. Using the following test, but using a release build... The results were backwards. In release buidl, the non-static was faster than the static. All I did to get the other way was to remove the static modifier.


    
#include <iostream>

void main()
{
for(int i = 0; i < 1000; i++)
{
static int tmp;
tmp = 0;
std::cout << tmp++ << std::endl;
}
}


Weird. Oh well, no point arguing. looks like the transients outway any gains by going one way or the other. Phase o' the moon, I guess.

Oh well,
Landsknecht

P.S. I was being very silly and descriptively dramatic when writing my posts. I thought it was obvious enough, guess not.

[edited by - landsknecht on March 19, 2002 4:11:01 AM]

Share this post


Link to post
Share on other sites