g++ std::max() performance

2007-09-08 Thread Hicham Mouline
With -O3, in a inner loop, that iterates perhaps 10^9 times,
using 
double opt[100];
for ()
 for (...)
opt[i] = std::max( opt[i-1] , opt[i-2] );

is much much slower than
for ()
 for (...)
opt[i] = opt[i-1]>opt[i-2]? opt[i-1]: opt[i-2];

Looking at the implementation std::max in stl_algobase.h
 template
inline const _Tp&
max(const _Tp& __a, const _Tp& __b)
{
  __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
  //return  __a < __b ? __b : __a;
  if (__a < __b)
return __b;
  return __a;
}

It is "inline" and the function arguments are const-references,
So shouldn't it be as fast as the ?: tri-operator version?

Can anyone else observe this?

Rds,
PS: I've noticed this on x86.
PS: I've come across some bug in Sun studio's compilers that seemed related,
but perhaps isn't:
http://bugs.sun.com/view_bug.do?bug_id=6498247



RE: g++ std::max() performance

2007-09-08 Thread Hicham Mouline
Right. I haven't used 4.3 yet.
I googled around and the only other compiler I saw that fixed this 
was SU's, in my previous post. (if I understood correctly)
They fixed it just a few months back.

tx
-Original Message-
From: Paolo Carlini [mailto:[EMAIL PROTECTED] 
Sent: 08 September 2007 16:38
To: Hicham Mouline
Cc: gcc@gcc.gnu.org
Subject: Re: g++ std::max() performance
Hicham Mouline wrote:
>Can anyone else observe this?
>  
>
You are observing it on gcc4.2.x or gcc4.1.x, right? Because mainline 
(would be 4.3.0) is able to optimize both exactly to the same tree. 
Actually, should be one of the very few compilers around implementing 
that optimization ;) See PRs tree-optimization/30738 (and 19431).
Paolo.