Efficiency question - variable definition
-
Does it matter where I define a variable? For example, take the following two cases. Does one result in more efficient execution code than the other? Variable x is defined in differnt places. Are there any general guidlines for this? Thanks. CASE 1
int x = 0; for(int i = 0; i< 10; i++) { x = a + b + c; }
CASE 2for(int i = 0; i< 10; i++) { int x = a + b + c; }
-
Does it matter where I define a variable? For example, take the following two cases. Does one result in more efficient execution code than the other? Variable x is defined in differnt places. Are there any general guidlines for this? Thanks. CASE 1
int x = 0; for(int i = 0; i< 10; i++) { x = a + b + c; }
CASE 2for(int i = 0; i< 10; i++) { int x = a + b + c; }
Case 2 defines it over and over. I've been told this can be *more* efficient, I suspect otherwise. The truth is, that sort of optimisation is rarely going to give you visible benefits
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Does it matter where I define a variable? For example, take the following two cases. Does one result in more efficient execution code than the other? Variable x is defined in differnt places. Are there any general guidlines for this? Thanks. CASE 1
int x = 0; for(int i = 0; i< 10; i++) { x = a + b + c; }
CASE 2for(int i = 0; i< 10; i++) { int x = a + b + c; }
When the variable doesn't have a ctor or a dtor (as in this case) it makes no difference in release mode - the optimizer will alloc space for the variable on the stack once. In debug builds,
x
will be reset to 0xCCCCCCCC every time through the loop, but speed isn't a concern in debug mode.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?
-
Case 2 defines it over and over. I've been told this can be *more* efficient, I suspect otherwise. The truth is, that sort of optimisation is rarely going to give you visible benefits
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Christian Graus wrote:
The truth is, that sort of optimisation is rarely going to give you visible benefits
Anytime you can optimize the better, I say. The compiler may or may not optimize it, why take the chance. Like Christian said, it probably won't matter in most sutuations, but if you're looping through a million rows it will.
- S 50 cups of coffee and you know it's on!
-
Does it matter where I define a variable? For example, take the following two cases. Does one result in more efficient execution code than the other? Variable x is defined in differnt places. Are there any general guidlines for this? Thanks. CASE 1
int x = 0; for(int i = 0; i< 10; i++) { x = a + b + c; }
CASE 2for(int i = 0; i< 10; i++) { int x = a + b + c; }
I always try to define a variable like that only one time (outside the loop). It shouldn't make a difference unless it's a more complex type that has a lengthy constructor/destructor. I'm old, and set in my ways.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001