Does declaring a variable cause lag in the executable?

This is a discussion on Does declaring a variable cause lag in the executable? within the C++ Programming forums, part of the General Programming Boards category; This came up in a thread, but I didn't want to hijack, since it has basically nothing to do with ...

  1. #1
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300

    Does declaring a variable cause lag in the executable?

    This came up in a thread, but I didn't want to hijack, since it has basically nothing to do with the OP's question.

    Since we're supposed to declare variables as locally as possible, does it matter, as far as performance goes, where the variables are declared and initialized? At the beginning of the code? Just before use? Does it depend on the kind of variable? The size of the variable? The size of the program?

    I know, as Salem put it, that premature optimization is a terrible disease, but any clarification in this matter would be greatly appreciated. Thanks.
    There is a difference between tedious and difficult.

  2. #2
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    nope, it's a single sub to declare a local variable, objects are much slower (put in the heap) but no performance hit is taken.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    I highly recommend checking out from a library or buying Sutter's and Alexandrescu's C++ Coding Standards. It covers almost all of these "style" issues that aren't just style issues but have an impact on your code's performance, maintainability, robustness, and flexibility. It is not just their opinions on these subjects either, it is a compilation of facts and the opinions (many of them published) of the best C++ minds today. If you're not sure which method is a best practice, start there, and follow up with the references for a more detailed discussion.

    As for this subject, basically they say "Declare variables as locally as possible." Doing so makes the program easier to understand and maintain (no need to worry about how a change will affect a variable's usage later on in the function). It allows you to postpone initialization until you have a good value to initialize with, which helps to avoid unitialized variable bugs and provides potential performance benefit by not calling expensive initialization code when it is not needed.

    As far as a lag in your program occurring because of variable declaration, this doesn't happen. In the rare case where you've identified a piece of code (be it an initialization or something else) that is causing a slowdown, you could of course then move it to an appropriate place in your program's execution flow. Otherwise it is a waste of time to even think about.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Daved
    As far as a lag in your program occurring because of variable declaration, this doesn't happen.
    I disagree in c++ code. True, elementary data types such as char, int, float, etc. it doesn't matter because all data objects, including c++ classes, are allocated at the same time upon function entry. But, c++ classes are a little different because the class constructors are also called. So declaring a c++ class earlier than needed can cause premature and unnecessry constructor and destrustor calls when the function returns before the class is needed.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    The discussion originated from a contention in another thread that variables should be declared at the beginning of a function so that the lag happens at the beginning instead of in the middle. My comment was in reference to that nonsense. I certainly agree with what you say.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C variable
    By webstarsandeep in forum C Programming
    Replies: 1
    Last Post: 10-23-2008, 01:26 AM
  2. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  3. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  4. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  5. Beginner question
    By Tride in forum C Programming
    Replies: 30
    Last Post: 05-24-2003, 08:36 AM

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