ctr++ or ++ctr

B

bob smith

Are these pieces of code identical?


------------

for (int ctr = 0; ctr < 100; ctr++)
{
// something
}

----------
for (int ctr = 0; ctr < 100; ++ctr)
{
// something
}

------------


If not, which is better?

Thanks.
 
T

Tobias Müller

bob smith said:
Are these pieces of code identical?


------------

for (int ctr = 0; ctr < 100; ctr++)
{
// something
}

----------
for (int ctr = 0; ctr < 100; ++ctr)
{
// something
}

In this specific case, they are equivalent.

Generally, ++ctr is the 'natural' increment operator IMO. It behaves like a
normal function without using additional temporaries.
When using operator overloading, the postfix increment operator (ctr++) is
often implemented in terms of ++ctr like this:

MyType MyType::eek:perator++()
{
MyType temp(*this);
++(*this);
return temp;
}

This involves an additional temporary object and is thus slower.

Tobi
 
B

Barry Schwarz

Are these pieces of code identical?


------------

for (int ctr = 0; ctr < 100; ctr++)
{
// something
}

----------
for (int ctr = 0; ctr < 100; ++ctr)
{
// something
}

Yes, they are identical in effect. Whether one is more efficient than
the other depends on the instruction set of your system and what
optimizations your compiler may implement.
 
B

Bo Persson

Tobias Müller skrev 2013-08-30 08:00:
In this specific case, they are equivalent.

Generally, ++ctr is the 'natural' increment operator IMO. It behaves like a
normal function without using additional temporaries.
When using operator overloading, the postfix increment operator (ctr++) is
often implemented in terms of ++ctr like this:

MyType MyType::eek:perator++(int)
{
MyType temp(*this);
++(*this);
return temp;
}

This involves an additional temporary object and is thus slower.

To be nitpicking:

Many compilers will optimize out the unused temporary and make this
exactly equivalent to the pre-increment version.

"Not faster" would be slightly more correct.


Bo Persson
 
P

Paul N

Are these pieces of code identical?

------------

for (int ctr = 0; ctr < 100; ctr++)
{
// something
}

----------

for (int ctr = 0; ctr < 100; ++ctr)
{
// something
}

------------

If not, which is better?

Thanks.

They are identical in effect and I would be surprised if they had any difference in speed. I think the former form looks more natural and so that is what I'd use.

That's assuming you haven't done anything strange with macros, of course...

Differences may arise if you are using a class (with overloaded operators) rather than an int. It is allowable, though extremely bad style, for obj++ and ++obj to do different things. Normally, though, ++obj will return a (const) reference to the thing itself whereas obj++ will in general need a temporary. So ++obj may be faster (it all depends on how clever the compiler is). And some people feel that, if it's best to use ++obj, then it's best touse ++ctr for ints as well for consistency. Which is a sensible view, but not one I hold.
 
F

Fred Zwarts \(KVI\)

"bob smith" wrote in message
Are these pieces of code identical?


------------

for (int ctr = 0; ctr < 100; ctr++)
{
// something
}

----------
for (int ctr = 0; ctr < 100; ++ctr)
{
// something
}

------------


If not, which is better?

Thanks.

It depends. What do you mean?
The two pieces are not identical. A simple diff will show you that there are
differences.
Is the effect the same? Yes, in this particular case, the effect is the
same.
Are they equally efficient? With a compiler with a good optimizer, equally
efficient code will be produced.
Are they equally clear? Not completely. The ctr++ operation is slightly more
complex than the ++ctr operation, although the additional complexity is not
used here. When reading such code, one may question why this additional
complexity is chosen. So, it a bit more questionable.
 
Ö

Öö Tiib

"Extremely bad style"? Nonsense.

Most intuitive in most contexts is that both operator++ increment the
operand. Prefix operator++ returns reference to operand and postfix
operator++ returns operands value prior to increment.

I also trust that it is extremely bad style to overload any operator to
do something not intuitive. Most readers of code are surprised and may
get confused so it wastes their time and so it is bad style.

I myself prefer prefix operator++ because it matches more how I think.
I read '++x' as "increment x" and 'x++' as "x, then increment it".
 
T

Tobias Müller

Bo Persson said:
Tobias Müller skrev 2013-08-30 08:00:

To be nitpicking:

Many compilers will optimize out the unused temporary and make this
exactly equivalent to the pre-increment version.

Only if the function can be inlined.

Also I'm not sure whether the compiler is actually allowed to optimize it
away, especially if the class has nontrivial copy constructor or
destructor.
"Not faster" would be slightly more correct.

There are certainly cases where there is no difference, that's true.

Tobi
 
P

Paul N

Nonsense.



You use whatever is appropriate for the task at hand. Overloading both

is perfectly fine. Prefix and postfix increment have been with us since

year dot of C.

Leigh - can you give an example of where obj++ and ++obj do different things (as opposed to simply returning different results) but this looks sensible to you?

I would argue that that, say, for obj++ to increase the value of obj by 1 and for ++obj to decrease it by 2 would be misleading, but presumably you would think this a stupid example, so do you have a better one?
 
S

Stefan Ram

bob smith said:
Are these pieces of code identical?
No.

If not, which is better?

On the Commodore Amiga, IIRC, some versions of the Lattice C
compiler generate more complicated code for »c++« than for
»++c«, therefore, I prefer »++c«. But other compilers might
exhibit other behaviors.

In English, we say »increment c«, not »c increment«, so »++c«
might be more readable to someone who is new to programming.

Many programmers tend to write »c++« without thinking, because
they have read it more often this way.
 
S

Stefan Ram

Leigh Johnston said:
The difference I am talking about is that they do return different results.

Expressions »have« values, they do not return values.

For example, when »int f(){ return 2; }«, we say:

- »f returns 2.«, but
- »f() has the value 2.« or »The value of f() is 2.«.

The »value of f« also exists, but it is not 2 (»f != 2«).
 
S

Stefan Ram

(...) ¯¯¯¯
Way to go with arguing pedantically over semantics; I can do that to: we
are actually talking about overloading of operator *functions* and
functions *return* results.

You can't do that too. »obj++« and »++obj«
are two expression, not to two functions.
 
S

Stefan Ram

(...) ¯¯¯¯
You can't do that too. »obj++« and »++obj«
are two expression, not to two functions.

Ok, I have read the long quotation (omitted above) again,
and now I see your point that it is actually possible that
»they« above might refer to the operator functions. Sorry.
 
J

James Kanze

Only if the function can be inlined.
Also I'm not sure whether the compiler is actually allowed to optimize it
away, especially if the class has nontrivial copy constructor or
destructor.
There are certainly cases where there is no difference, that's true.

The question is more: are there any realistic cases where it
makes a difference. When I did my measurements, I was unable to
find any. (You can, of course, artificially construct iterators
where it does make a difference. Either way, for that matter.
But such iterators are perverse, and would probably not work
well in practice anyway, given the frequency the standard
library copies iterators.)
 
J

James Kanze

Most intuitive in most contexts is that both operator++ increment the
operand. Prefix operator++ returns reference to operand and postfix
operator++ returns operands value prior to increment.

If they did anything else, that would be a flagrant case of
operator overloading abuse.
I also trust that it is extremely bad style to overload any operator to
do something not intuitive. Most readers of code are surprised and may
get confused so it wastes their time and so it is bad style.
I myself prefer prefix operator++ because it matches more how I think.
I read '++x' as "increment x" and 'x++' as "x, then increment it".

That's a valid point of view. I suspect a lot of older C/C++
programmers prefer x++, because that's what K&R used. Or
because the PDP-11 had a postfix increment address mode, which
may have unconsciously influenced their point of view (although
even the earliest C compilers generated exactly the same code if
you didn't use the results).
 
J

James Kanze

On the Commodore Amiga, IIRC, some versions of the Lattice C
compiler generate more complicated code for »c++« than for
»++c«, therefore, I prefer »++c«. But other compilers might
exhibit other behaviors.

That's interesting. The Lattice C compiler for 8086 (sold under
the name Microsoft C 1.0) generated exactly the same code. As
did pcc for the PDP-11.

Of course, if the return value was used, then differences might
appear. On the PDP-11, for example, *p++ would be a single
instruction; *++p required dereferencing first and storing the
results in a register. (On a PDP-11, *p ++ = * q++ would only
require a single machine instruction. I don't know whether the
compilers were actually able to find this, however.) But if the
results of ++p or p++ weren't used, the compilers generated
exactly the same code.
 
S

Stefan Ram

James Kanze said:
That's interesting. The Lattice C compiler for 8086 (sold under
the name Microsoft C 1.0) generated exactly the same code. As
did pcc for the PDP-11.

I do not have any means, right now, to verify whether my
memory is correct. But I indeed refer even to situations
where the value is not used.
 
B

bob smith

I do not have any means, right now, to verify whether my

memory is correct. But I indeed refer even to situations

where the value is not used.

The main reason I asked is because I saw the top YouTube video here:

https://www.youtube.com/results?search_query=prodigy+dreams+in+code

This prodigy uses ++i instead of i++, which seems to defy convention.

Anyhow, I guess the verdict is that ++i may generate slightly faster code.

Maybe the language ought to just be called ++C.

Thanks.
 
Ö

Öö Tiib

The main reason I asked is because I saw the top YouTube video here:

https://www.youtube.com/results?search_query=prodigy+dreams+in+code

This prodigy uses ++i instead of i++, which seems to defy convention.

What convention? The FAQ says that '++i' is never slower than 'i++'.
http://www.parashift.com/c++-faq/increment-pre-post-speed.html
That feels enough to suggest to prefer '++i' when both fit into situation.
Anyhow, I guess the verdict is that ++i may generate slightly faster code..

I haven't observed any actual differences in real programs. It
may be because compilers optimize so heavily. To me '++i' only feels better
to read ("increment i") and that is it.
Maybe the language ought to just be called ++C.

These two things are not related; it is pronounced "sea plus plus", not
"increment sea".
 
J

Jorgen Grahn

What convention? The FAQ says that '++i' is never slower than 'i++'.
http://www.parashift.com/c++-faq/increment-pre-post-speed.html
That feels enough to suggest to prefer '++i' when both fit into situation.

i++ is a convention in the sense that:
- it's a convention in C; look at any for(...) loop
- there are people who prefer to inherit into C++ too (including some
experienced c.l.c++ regulars[1])

Of course this is not a /universal/ convention. We have few of those!

/Jorgen

[1] Based on a discussion here a few years ago, and I don't recall any
names. I argued for ++i, but was convinced my arguments didn't hold.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top