Stack Variable and Speed

This is a discussion on Stack Variable and Speed within the C++ Programming forums, part of the General Programming Boards category; hi Does creating temporary variable inside for loop affects the execution speed....

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    32

    Stack Variable and Speed

    hi
    Does creating temporary variable inside for loop affects the execution speed.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Try it and see. (I would expect the answer would depend on what you do with it, how impressive the variable is (a char vs. a gargantuan class object), and what your optimization settings are.)

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    Maybe no
    Code:
    for ( i = 0 ; i < 10 ; i++ ) {
      int j = i * 10;
    }
    Maybe yes
    Code:
    for ( i = 0 ; i < 10 ; i++ ) {
      myClassWithExpensiveCtorDtor j(i);
    }
    It's generally not an issue for POD types.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    Try it and see
    I had tried creating an int variable inside a for loop ,storing a value in the variable and then print it.But every time the program takes different time to complete execution.

    creating a temporary variable inside a for loop is better or outside the for loop is better if the loop runs for many cycles.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by babu198649 View Post
    I had tried creating an int variable inside a for loop ,storing a value in the variable and then print it.But every time the program takes different time to complete execution.
    Well, yeah. But if you run it, say, 100 (1000? 100000?) you should be able to get a good average. (Obviously you don't run the thing 100 times and write down all the numbers; you make the program do that.)

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Quote Originally Posted by babu198649 View Post
    I had tried creating an int variable inside a for loop ,storing a value in the variable and then print it.But every time the program takes different time to complete execution.
    That is a downside to how operating systems work.
    The usual solution is tabstop's method.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,011
    Creating temporairy variables explicitly should not affect the execution speed. So the following should be the same:
    Code:
    {
      result+= array1[i]+array2[i]+array3[i];
    }
    
    {
        int partialSum=array1[i]+array2[i];
        result+= partialSum+array3[i];
    }
    In fact, I'd expect it to compile to the exact same code.

    Is this what you mean?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    Thanks to all for replys,

    I want to know wheater allocating temporary variables inside loop takes more cpu cycles(since pushing each time the varible in to stack and deleting when the scope is out) than allocating the variable ouside the loop.
    Code:
    {
      result+= array1[i]+array2[i]+array3[i];
    }
    
    {
        int partialSum=array1[i]+array2[i];
        result+= partialSum+array3[i];
    }
    In the second part of the code the variable partialSum has to be created when the execution enters the brace and it has to be destroyed when the variable goes out of scope,ie.taking more cycles than the first part of code.

    Does both parts of the code above takes same number of cpu cycles?

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,011
    Quote Originally Posted by babu198649 View Post
    Thanks to all for replys,

    I want to know wheater allocating temporary variables inside loop takes more cpu cycles(since pushing each time the varible in to stack and deleting when the scope is out) than allocating the variable ouside the loop.
    Code:
    {
      result+= array1[i]+array2[i]+array3[i];
    }
    
    {
        int partialSum=array1[i]+array2[i];
        result+= partialSum+array3[i];
    }
    In the second part of the code the variable partialSum has to be created when the execution enters the brace and it has to be destroyed when the variable goes out of scope,ie.taking more cycles than the first part of code.

    Does both parts of the code above takes same number of cpu cycles?
    In either case you need a place to store the partial sum before the calculation. In the second case that place is named, that is all.

    On really old C compilers it used to be that you had to use the register key word to make sure they are treated the same way, but you shouldn't do that on modern compilers.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    since pushing each time the varible in to stack and deleting when the scope is out
    You're taking this way too literally. This is a conceptual view of variables and has little to do with the code a compiler actually generates.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Quote Originally Posted by CornedBee View Post
    You're taking this way too literally. This is a conceptual view of variables and has little to do with the code a compiler actually generates.
    Indeed. To elaborate though:
    It doesn't matter where inside a function a variable is declared, space for it is allocated upon entry to the function. The only sense in which a variable comes into existence during the function execution is that at some point its constructor must be run, unless of course it is a POD type in which case there is nothing extra to do.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    This seems like a thing that the compiler would optimize if it thought that it was going to cause slow down. I usually initialize variables right before I use them as that is what Effective C++ recommends.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,011
    Quote Originally Posted by iMalc View Post
    Indeed. To elaborate though:
    It doesn't matter where inside a function a variable is declared, space for it is allocated upon entry to the function. The only sense in which a variable comes into existence during the function execution is that at some point its constructor must be run, unless of course it is a POD type in which case there is nothing extra to do.
    Except in my example a compiler would most likely use a register, which would remove a step of writing to and reading from the stack.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Quote Originally Posted by King Mir View Post
    Except in my example a compiler would most likely use a register, which would remove a step of writing to and reading from the stack.
    Oh of course, even better.
    babu198649, basically just follow best practices, and trust that the compiler knows best.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    yes its slower than a for loop that uses a pre allocated variable, since one spends time allocating the variable and one does not, but the difference is on the order of a few clock cycles at most. I would avoid it if its part of an inner loop, but if its part of the outer loop, or a solitary loop, I wouldnt worry about it.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am very new . . . :(
    By Eternalglory47 in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2008, 11:29 AM
  2. Resource Management question..
    By Raigne in forum C++ Programming
    Replies: 37
    Last Post: 03-08-2008, 08:36 AM
  3. Replies: 6
    Last Post: 01-08-2006, 01:49 PM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  5. Replies: 5
    Last Post: 06-01-2002, 11:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21