-
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?).
-
Short answer: None. No difference.
Long Answer: I dont have time.
-
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.
-
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.
-
Ok, thanks everyone! And thanks, trollking, I guess I'll look into that book :)