Re: [eigen] prefix vs. postfix increment |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] prefix vs. postfix increment
- From: "Benoit Jacob" <jacob.benoit.1@xxxxxxxxx>
- Date: Wed, 17 Dec 2008 15:33:42 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=R0ReYQl1rUaORq8w9N9EjFtQlTuvxUSwPNYa6x5ClBg=; b=XO2fvdGhzHoGQFCpm42b8HjZ/WNZ5E9MdrR5ScpLRkEYA/Lf7uLkMiL+duhm6iG/2o +Jr24cF/SD60NKF8CJdyv1uStGoD0COCWdcO0LS5urUWw68d7KjWoDyL2m86UA/FwpG2 zDdJhS5rAWes0eFbb8LGs2kR6+voRlvnadSJk=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=GT1XgVlr3GbhHwQxXrGGhC27COiyvAo5RRcicH6MEuBm9asC/drdGNvsbDrDyeoMng HcheyUo3HixurhjfU/b4Gzs7u1O27wFdkCBxrp91CUJ98jfg3cccGtWACtRyJx3ZCdQQ OdXvWFxQgC2t3KMYs+ZSwIcrXnuPtkpZzhc34=
Thanks a lot for the explanation -- it's something that I never
understood that clearly.
Looking at Eigen, it seems that the only postfix++ that we had were on
ints, where the compiler is probably able to optimize. Nevertheless I
replaced postfix++ by prefix++ wherever that makes sense in Eigen/ ,
so it looks more homogeneous.
Cheers,
Benoit
2008/12/16 Kenneth Riddile <kfriddile@xxxxxxxxx>:
> This might sound nitpicky, but I think it should be mentioned since performance seems to be one of the main goals of Eigen (and it's something everyone should do anyways). I've noticed when looking through the source code, that postfix increment is being used in places where prefix increment would suffice. Some compilers will automatically fix this for you when possible, but many times they can't due to the possibility of overloaded operators with user-defined types. Also, some compilers just don't do it even with basic types, depending on optimization settings. What I'm talking about is this:
>
> for(int j = 0; j < cols(); j++)
> for(int i = 0; i < rows(); i++)
>
> should be:
>
> for(int j = 0; j < cols(); ++j)
> for(int i = 0; i < rows(); ++i)
>
> The reason is that postfix increment needs to return the original value, which requires it to cache that value in a temporary variable, incurring a copy (which is even more expensive with a complex type like an STL iterator). To make the performance difference more clear, think about how you would overload both operators for some class "Integer":
>
> // Prefix operator
> Integer Integer::operator++()
> {
> return ( *this + 1 );
> }
>
> // Postfix operator
> // Note that C++ discerns between the prefix and postfix
> // overloads by requiring the postfix overload to have an
> // unused "dummy" integer parameter...a kludge if I ever saw one.
> Integer Integer::operator++( int dummy )
> {
> Integer previousValue = *this;
>
> *this = *this + 1;
>
> return previousValue;
> }
>
> ---
>
>
---