Whats faster? i1++ or ++i1

T

thebrath

I'm wondering if I will get faster code in any case if I use i1++.

Do you have any experiences?
 
P

Phlip

thebrath said:
I'm wondering if I will get faster code in any case if I use i1++.

If il++ calls an overloaded operator++(int), and if that is slower than
operaor++() (the ++il form), then it will be slower.

As a style thing, get in the habit of always typing ++il because it's
"weaker" than il++. It has less features.

That rule is just to make ++il easier to understand. The built-in version of
il++ can easily be optimized as fast as ++il.

Google for "premature optimization" now.
 
A

Alan Johnson

Phlip said:
If il++ calls an overloaded operator++(int), and if that is slower than
operaor++() (the ++il form), then it will be slower.

As a style thing, get in the habit of always typing ++il because it's
"weaker" than il++. It has less features.

That rule is just to make ++il easier to understand. The built-in version of
il++ can easily be optimized as fast as ++il.

Google for "premature optimization" now.

Your question is in the FAQ. Please see:
http://www.parashift.com/c++-faq-lite/
 
N

Noah Roberts

thebrath said:
I'm wondering if I will get faster code in any case if I use i1++.

Do you have any experiences?

Always use the most generic method with the least amount of
requirements and does the least amount of work as possible. i++
requires the original value of i to be returned. In the case of
iterators this means returning a copy...it does more work than is
necessary for the task unless you really need the previous value. ++i
is more generic, has less requirements, and does less work...use it
when you can, which is most of the time in my experience.

The reason you do this is so that if you change what i is, so long as
it has the correct interface, it will work and be as fast as it can be.
Don't optimize prematurely, but don't pesimize prematurely either.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top