Possible inefficiency in post-increment FOR loop?
-
I was reading a coding style document put out by a certain company, when I came across an odd claim in their justification for their FOR loop style. I was wondering if someone could shed some light on this for me: The document states that a pre-increment operator is preferable to a post-increment operator because post-increment introduces potential inefficiencies when iterating over an object. The example they give is C++ code:
std::vector v;
// vector gets filled here....
for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i)
{
// iteration code here...
}...and an identical sample that uses "i++" instead of "++i" for the increment operation. I see no difference in the efficiency. Whether using ++i or i++, "i" is going to contain its new value before the termination condition is evaluated. The loop will be iterated an identical number of times. I was wondering if someone could shed some light on what it is that I'm missing here, or if perhaps the assertion that pre-increment is more efficient. Is there some other language or platform that treats identical FOR loop sytax in a wildly different manner?
-
I was reading a coding style document put out by a certain company, when I came across an odd claim in their justification for their FOR loop style. I was wondering if someone could shed some light on this for me: The document states that a pre-increment operator is preferable to a post-increment operator because post-increment introduces potential inefficiencies when iterating over an object. The example they give is C++ code:
std::vector v;
// vector gets filled here....
for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i)
{
// iteration code here...
}...and an identical sample that uses "i++" instead of "++i" for the increment operation. I see no difference in the efficiency. Whether using ++i or i++, "i" is going to contain its new value before the termination condition is evaluated. The loop will be iterated an identical number of times. I was wondering if someone could shed some light on what it is that I'm missing here, or if perhaps the assertion that pre-increment is more efficient. Is there some other language or platform that treats identical FOR loop sytax in a wildly different manner?
i++;
/*
* reults in:
* load i to save location
* increment i
* return saved i
*/++i;
/*
* reults in:
* increment i
* return i
*/In reality most modern compilers will optimise the code so it is of little consequence to the majority of programmers. It only becomes an issue if your program needs to run at sub-atomic speeds.
Just say 'NO' to evaluated arguments for diadic functions! Ash
-
I was reading a coding style document put out by a certain company, when I came across an odd claim in their justification for their FOR loop style. I was wondering if someone could shed some light on this for me: The document states that a pre-increment operator is preferable to a post-increment operator because post-increment introduces potential inefficiencies when iterating over an object. The example they give is C++ code:
std::vector v;
// vector gets filled here....
for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i)
{
// iteration code here...
}...and an identical sample that uses "i++" instead of "++i" for the increment operation. I see no difference in the efficiency. Whether using ++i or i++, "i" is going to contain its new value before the termination condition is evaluated. The loop will be iterated an identical number of times. I was wondering if someone could shed some light on what it is that I'm missing here, or if perhaps the assertion that pre-increment is more efficient. Is there some other language or platform that treats identical FOR loop sytax in a wildly different manner?
Their statement is utter non-sense, as Richard already explained. And furthermore they should concentrate on the termination test of the for loop;
v.end()
is getting evaluated over and over, not necessarily what is required (depends on how "alive" v is). :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Season's Greetings to all CPians.
-
Their statement is utter non-sense, as Richard already explained. And furthermore they should concentrate on the termination test of the for loop;
v.end()
is getting evaluated over and over, not necessarily what is required (depends on how "alive" v is). :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Season's Greetings to all CPians.
Yeah, I HATE seeing that in loops. Coders think its efficient because they have less code without thinking that a function call in the conditional part of the loop gets called every itteration.
"It is a remarkable fact that despite the worldwide expenditure of perhaps US$50 billion since 1990, and the efforts of tens of thousands of scientists worldwide, no human climate signal has yet been detected that is distinct from natural variation." Bob Carter, Research Professor of Geology, James Cook University, Townsville
-
I was reading a coding style document put out by a certain company, when I came across an odd claim in their justification for their FOR loop style. I was wondering if someone could shed some light on this for me: The document states that a pre-increment operator is preferable to a post-increment operator because post-increment introduces potential inefficiencies when iterating over an object. The example they give is C++ code:
std::vector v;
// vector gets filled here....
for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i)
{
// iteration code here...
}...and an identical sample that uses "i++" instead of "++i" for the increment operation. I see no difference in the efficiency. Whether using ++i or i++, "i" is going to contain its new value before the termination condition is evaluated. The loop will be iterated an identical number of times. I was wondering if someone could shed some light on what it is that I'm missing here, or if perhaps the assertion that pre-increment is more efficient. Is there some other language or platform that treats identical FOR loop sytax in a wildly different manner?
I was going to say you should take a look at this code disassembled to see if there is any inefficiency, but Richard has answered that already.
"It is a remarkable fact that despite the worldwide expenditure of perhaps US$50 billion since 1990, and the efforts of tens of thousands of scientists worldwide, no human climate signal has yet been detected that is distinct from natural variation." Bob Carter, Research Professor of Geology, James Cook University, Townsville
-
I was reading a coding style document put out by a certain company, when I came across an odd claim in their justification for their FOR loop style. I was wondering if someone could shed some light on this for me: The document states that a pre-increment operator is preferable to a post-increment operator because post-increment introduces potential inefficiencies when iterating over an object. The example they give is C++ code:
std::vector v;
// vector gets filled here....
for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i)
{
// iteration code here...
}...and an identical sample that uses "i++" instead of "++i" for the increment operation. I see no difference in the efficiency. Whether using ++i or i++, "i" is going to contain its new value before the termination condition is evaluated. The loop will be iterated an identical number of times. I was wondering if someone could shed some light on what it is that I'm missing here, or if perhaps the assertion that pre-increment is more efficient. Is there some other language or platform that treats identical FOR loop sytax in a wildly different manner?
As Richard and Luc have explained, there is no difference in this case. What they MAY have been referring to is this:
a[i++] = x;
b[++j] = z;In a primitive CPU, the code might compile to something like this (much liberties taken in pseudo assembler code!)
load r1,i
add r1,=a
load r2,x
store r2,(r1)
load r1,i
inc r1
store r1,iload r1,j
inc r1
store r1,j
add r1,=b
load r2,z
store r2,(r1)In the early days of C, there were machines that stupid, but by the time C++ came along, the "issue" was long gone. Essentially, it's about using
++i
in an expression compared to usingi++
. If the value of the side effect isn't used, there's no real difference. Cheers, PeterSoftware rusts. Simon Stephenson, ca 1994.
-
I was reading a coding style document put out by a certain company, when I came across an odd claim in their justification for their FOR loop style. I was wondering if someone could shed some light on this for me: The document states that a pre-increment operator is preferable to a post-increment operator because post-increment introduces potential inefficiencies when iterating over an object. The example they give is C++ code:
std::vector v;
// vector gets filled here....
for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i)
{
// iteration code here...
}...and an identical sample that uses "i++" instead of "++i" for the increment operation. I see no difference in the efficiency. Whether using ++i or i++, "i" is going to contain its new value before the termination condition is evaluated. The loop will be iterated an identical number of times. I was wondering if someone could shed some light on what it is that I'm missing here, or if perhaps the assertion that pre-increment is more efficient. Is there some other language or platform that treats identical FOR loop sytax in a wildly different manner?
Pre-increment's return value is the iterator itself, while post-increment must return a copy of the iterator before it has been incremented. Since the
for
loop discards the return value, using the pre-increment version of the++
operator saves some CPU cycles by not making a copy that will be discarded anyway. A longer explanation is here.[^] Edit: Oops, I did not see that there were so many replies already. Only the OP was visible at the bottom of the forums window.