Thursday, June 11, 2009

Optimizing Infinite Loops - Stack Overflow C, C++ question

There was a rather amusing question posted on Stack Overflow:

I am just wondering if there would be any loss of speed or efficiency if you did something like this:

int i = 0;
while(i < 100)
{
int var = 4;
}

which declares int var one hundred times. It seems to me like there would be, but I'm not sure. would it be more practical/faster to do this instead:

int i = 0;
int var;
while(i < 100)
{
var = 4;
}

My Answer: Both looks take 2 bytes of space and an infinite amount of time :)

No comments:

Post a Comment