Re: [reSIProcate] Minor suggestion to use ++i instead of i++
Hi,
For built in types, whether pre or post increment is used doesn't matter much
at all. Where it matters from an efficiency point of view is when pre and
post increment operators are defined for a user defined type. When you don't
care about the return value, and only about incrementing, use the
preincrement form. Using the post-increment form when you don't care about
the return value will cause the unnecessary saving of the old value (before
incrementing) which doesn't get used. Since it doesn't matter which form is
technically used for built in types one way or the other, using the
pre-increment form is just a matter of habit forming for when it does matter
(when UDT are used). Consider the following typical example.
Cheers,
Jon
#include <iostream>
#include <cassert>
using namespace std;
class Rational {
long Num, Den; // Rational is Num/Den
void makerat(long n, long d);
long gcd(long a, long b);
public:
Rational(long n = 0, long d = 1);
bool operator<(const Rational&) const;
const Rational &operator++(); // Preincrement
Rational operator++(int); // Postincrement
void print(ostream &os);
};
Rational::Rational(long n, long d) {
assert( d != 0 ); // Halt if d == 0.
if ( d < 0 ) d = -d, n = -n;
makerat(n, d);
}
bool Rational::operator<(const Rational& other) const
{
return (Num/Den) < (other.Num/other.Den);
}
const Rational &Rational::operator++()
{
makerat(Num + Den, Den);
return *this;
}
Rational Rational::operator++(int)
{
Rational oldvalue(*this);
makerat(Num + Den, Den);
return oldvalue;
}
void Rational::makerat(long n, long d)
{
long divisor = (n < 0) ? gcd(-n, d) : gcd(n, d);
Num = n / divisor;
Den = d / divisor;
}
long Rational::gcd(long a, long b)
{
long alpha, beta, temp;
if ( a > b ) { alpha = a; beta = b; }
else { alpha = b; beta = a; }
if (beta==0) beta = 1;
while ( (temp = alpha % beta) != 0 ) {
alpha = beta; beta = temp;
}
return beta;
}
void Rational::print(ostream &os)
{
os << Num;
if ( Den != 1 ) os << '/' << Den;
}
int main()
{
Rational r;
Rational limit = 100;
for (r = 0; r < limit; ++r) {
r.print(cout); cout << endl;
}
return 0;
}
On Saturday 06 November 2004 04:41 pm, Cullen Jennings wrote:
> I took the following program
>
> int main()
> {
> int i,j;
> j=0;
> for( i=0; i<100; i++ )
> {
> j = j+5;
> }
>
> return j;
> }
>
> and compiled it on a few different compilers with ++I and I++ and in
> *every* case, I got executables that bit wise compared to exactly the same
> thing.
>
> I suspect that this pre/post increment it total folk lore that is no longer
> true on any modern compiler.
>
> On 11/4/04 4:37 PM, "Alan Hawrylyshen" <alan@xxxxxxxxxx> wrote:
> > On Nov 4, 2004, at 15.58, kaiduan xie wrote:
> >> Hi, all,
> >>
> >> After reviewing the source code, I found a lot of
> >> places using i++ in loop of iterators, just to suggest
> >> use ++i instead. Thanks,
> >>
> >> kaiduan
> >
> > I would tend to agree -- if you are editing some code that's doing an
> > post-increment on an iterator, it's work changing to to pre-increment
> > if there is no call for the value before incrementing.
> >
> >
> >
> >
> > a l a n a t j a s o m i d o t c o m
> >
> > _______________________________________________
> > resiprocate-devel mailing list
> > resiprocate-devel@xxxxxxxxxxxxxxxxxxx
> > https://list.sipfoundry.org/mailman/listinfo/resiprocate-devel
>
> _______________________________________________
> resiprocate-devel mailing list
> resiprocate-devel@xxxxxxxxxxxxxxxxxxx
> https://list.sipfoundry.org/mailman/listinfo/resiprocate-devel