Thread: local variables performance

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    local variables performance

    I have the following code:

    Code:
    for (i = 0; i < 1000000; i++) {
      int Side1 = Result[i].X1 - 1;
      int Side2 = Result[i].X2 - 1;
    
      double Dev1 = Func(Idx, Side1);
      double Dev2 = Func(Idx + 1, Side2);
    
      .....
    }
    Will it perform better if I take the declarations of the doubles and ints out of the loop? In the general sense.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Probably not. Compilers are very adept at making these kinds of optimizations.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by KIBO View Post
    Will it perform better if I take the declarations of the doubles and ints out of the loop? In the general sense.
    In a general sense, it would be more efficient to declare the variables outside the loop -- if, instead of being basic data types, they were class objects, then the class constructor/destructor would be called for each pass through the loop.

    In this specific case, it doesn't make any difference.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could try it both ways and see for yourself.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by cpjust View Post
    You could try it both ways and see for yourself.
    I could but then I would know for one compiler, 1 setting!

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by KIBO View Post
    I could but then I would know for one compiler, 1 setting!
    Yep .... this sort of thing is dependent upon compiler, compiler setting/optimisation, operating system, hardware ...... and the interaction between them.

    If the answer really matters in your application (which is unlikely, BTW: it's more likely you're indulging in premature optimisation) then you need to test for every relevant use case.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by grumpy View Post
    If the answer really matters in your application (which is unlikely, BTW: it's more likely you're indulging in premature optimisation) then you need to test for every relevant use case.
    Or at least for a couple of the most important cases. If your code is very portable, it can be hard to find the right solution for LOTS of platforms without resorting to LOTS of #ifdef.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I don't see how in this case it could ever be a performance hit to lift the declaration out of the loop.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by KIBO View Post
    I could but then I would know for one compiler, 1 setting!
    That's a given for any non-algorithmic performance optimization.
    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

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    I don't see how in this case it could ever be a performance hit to lift the declaration out of the loop.
    I'd agree with that. But there is no end to what (stupidity) some compilers will perform.

    Quote Originally Posted by CornedBee View Post
    That's a given for any non-algorithmic performance optimization.
    And that is also something I'd agree with.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by brewbuck View Post
    I don't see how in this case it could ever be a performance hit to lift the declaration out of the loop.
    I don't recall anyone saying there was. However, the original question was the reverse: whether doing so would give a performance gain - for which there is no guarantee, particularly with optimising compilers.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. how to access local variables outside loops?
    By cdkiller in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2006, 06:48 PM
  4. regarding variables.
    By kiranck007 in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 07:30 AM
  5. global and local variables
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 01:17 PM