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