Performance difference?

This is a discussion on Performance difference? within the C++ Programming forums, part of the General Programming Boards category; Hey everybody! Just a quick question... Is there a difference in performance if you declare/define a variable at the beginning ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Performance difference?

    Hey everybody! Just a quick question... Is there a difference in performance if you declare/define a variable at the beginning of a function rather than in the middle of one? I'm just wondering because in C you have to declare them all at the beginning (right?).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    Short answer: None. No difference.

    Long Answer: I dont have time.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Actually, it can be more costly to declare them at the top, if you don't know what you want to put in them yet. If that is the case, the object is default constructed at the beginning of the scope, and then reassigned later. Often, it can simply be constructed from what it was reassigned to. Instead of a default constructor call and an assignment, it can be simplified to a single constructor call.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Many C++ performance questions are answered in a book called 'Efficient C++'. Ofcourse I have this book, but I have not read it. I'm still learning the general language and STL, but this book is one that can help you fine tune your skills, especially if you have all the core langauge elments under your belt.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, thanks everyone! And thanks, trollking, I guess I'll look into that book
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Performance and footprint of virtual function
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-31-2008, 06:34 PM
  2. File map performance
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2008, 03:18 AM
  3. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  4. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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