gpderetta
9/11/2008 9:56:00 PM
On Sep 11, 6:39 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> gpderetta wrote:
> > Why? There is no reason for the compiler not to transform pow(x,
> > <integral-constant>) to the latter form if it were actually faster
> > (and in fact some compilers do).
>
> Some compilers might be able to do that optimizations, others aren't.
> And if n is a variable, then it cannot optimize it.
and if it is variable, you can't write an explicit expression either.
You could use a for loop
> (At most the pow()
> function itself might have optimizations in it, but in my experience it
> doesn't: With most compilers it just generates the FPU opcodes necessary
> to calculate the result.)
Today hand optimizations are tomorrow pessimizations. Let the compiler
do its job.
The usual rule apply: use pow, and only if the profiler tells it is a
bottleneck, try to optimize it by hand.
>
> But we don't have to speculate about this as it's trivially easy to
> test in practice. Go ahead and try it.
I had already tried. 'pow(x, 16)' is inlined exactly as four
multiplies, at least with a recent gcc.
--
gpd