|
-
July 30th, 2005, 01:18 AM
#3
Re: C++ language: Why should I use ++i instead of i++ ?
 Originally Posted by C++ Newbie
[Andreas]: The following question is asked in regard to the following FAQ:
You mention that for regular variables (like int) there is no performance gain. Is this statement true for C++ but not for C? I've come across several people who do ++i all the time rather than i++ (in C) claiming efficiency gains.
normally it's a question of whether your compiler is able to "take a look" inside operator's implementation and optimise away a temporary variable if it's really not needed (not used by the caller code). for native compiler types it's possible and, more to say, with a clever compiler it's even possible with a user defined type under some circumstances.
e.g. when class's operator++ (int) is defined with "inline" specificator.
the recent c compilers do this optimization (at least, I have checked it out with gcc 3.3.4). what's about using ++i instead of i++? I guess it becomes a matter of habbit with time. e.g. if I know that I donot need an old value of the variable, I just don't use i++ even for for-loops.
"UNIX is simple; it just takes a genius to understand its simplicity!"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|