[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

When to use std::pow(x,n) instead of times x for n times?

Peng Yu

9/10/2008 12:30:00 AM

Hi,

I'm wondering if there is any general guideline on when to using
something like
std::pow(x, n)
rather than
x * x * x * ... * x (n x's).

Thanks,
Peng
24 Answers

Alf P. Steinbach

9/10/2008 1:12:00 AM

0

* Peng Yu:
> Hi,
>
> I'm wondering if there is any general guideline on when to using
> something like
> std::pow(x, n)
> rather than
> x * x * x * ... * x (n x's).

If you have more than 3 x's then that expression is going to be hard to read, so
then use pow. Focus on clarity.


Cheers & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Jack Klein

9/10/2008 3:42:00 AM

0

On Tue, 9 Sep 2008 17:30:04 -0700 (PDT), Peng Yu <PengYu.UT@gmail.com>
wrote in comp.lang.c++:

> Hi,
>
> I'm wondering if there is any general guideline on when to using
> something like
> std::pow(x, n)
> rather than
> x * x * x * ... * x (n x's).
>
> Thanks,
> Peng

In addition to what Alf said, here's my advice:

ALWAYS use std::pow(x, n) instead of trying to multiply 'x' by
multiple times when 'n' is not integral.

--
Jack Klein
Home: http://JK-Tech...
FAQs for
comp.lang.c http://...
comp.lang.c++ http://www.parashift.com/c++...
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-...

jellybean stonerfish

9/10/2008 3:58:00 AM

0

On Tue, 09 Sep 2008 17:30:04 -0700, Peng Yu wrote:

> Hi,
>
> I'm wondering if there is any general guideline on when to using
> something like
> std::pow(x, n)
> rather than
> x * x * x * ... * x (n x's).
>
> Thanks,
> Peng

It can be hard to represent fractional powers in x*x notation.

sf

Juha Nieminen

9/10/2008 2:24:00 PM

0

Peng Yu wrote:
> I'm wondering if there is any general guideline on when to using
> something like
> std::pow(x, n)
> rather than
> x * x * x * ... * x (n x's).

If you need to calculate that function millions of times per second,
then using the latter form can be considerably faster up to a certain n
(after which std::pow() becomes faster). The maximum n for which the
latter form is faster than std::pow() can be surprisingly large,
depending on the code (eg. something like n=8 might not be far-fetched).

Of course this is heavily system-dependent so there's no rule.

gpderetta

9/10/2008 3:54:00 PM

0

On Sep 10, 4:24 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Peng Yu wrote:
> > I'm wondering if there is any general guideline on when to using
> > something like
> > std::pow(x, n)
> > rather than
> > x * x * x * ... * x (n x's).
>
>   If you need to calculate that function millions of times per second,
> then using the latter form can be considerably faster up to a certain n
> (after which std::pow() becomes faster).

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).

--
gpd

stan

9/11/2008 1:19:00 AM

0

Juha Nieminen wrote:
> Peng Yu wrote:
>> I'm wondering if there is any general guideline on when to using
>> something like
>> std::pow(x, n)
>> rather than
>> x * x * x * ... * x (n x's).
>
> If you need to calculate that function millions of times per second,
> then using the latter form can be considerably faster up to a certain n
> (after which std::pow() becomes faster). The maximum n for which the
> latter form is faster than std::pow() can be surprisingly large,
> depending on the code (eg. something like n=8 might not be far-fetched).
>
> Of course this is heavily system-dependent so there's no rule.

It's always best to profile prior to optimization attempts. Rules of
thumb and "common sense" are frequently wrong; measurements seldom are
seldom wrong.

Michael DOUBEZ

9/11/2008 9:04:00 AM

0

Juha Nieminen a écrit :
> Peng Yu wrote:
>> I'm wondering if there is any general guideline on when to using
>> something like
>> std::pow(x, n)
>> rather than
>> x * x * x * ... * x (n x's).
>
> If you need to calculate that function millions of times per second,
> then using the latter form can be considerably faster up to a certain n
> (after which std::pow() becomes faster). The maximum n for which the
> latter form is faster than std::pow() can be surprisingly large,
> depending on the code (eg. something like n=8 might not be far-fetched).
>
> Of course this is heavily system-dependent so there's no rule.

In fact, there are some algorithms that are faster than others to
compute powers of natural number (by example derived from the russian
peasant multiplication).

I expect c++ libraries have a specialization with natural number as
second argument that gives better results in general.

If n is known at compile time, it is not hard to implement a template
with the russian paysan algorithm (the depth of instantiation recursion
should be sizeof(long)).

--
Michael

Juha Nieminen

9/11/2008 4:40:00 PM

0

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. (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.)

But we don't have to speculate about this as it's trivially easy to
test in practice. Go ahead and try it.

gpderetta

9/11/2008 9:56:00 PM

0

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

Juha Nieminen

9/12/2008 8:14:00 AM

0

gpderetta wrote:
>> 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

In my experience even performing a set of multiplications in a loop
while interpreting bytecode can be faster than a single std::pow() call,
up to a certain exponent.

I have made a function parser/interpreter, and in practice eg.
interpreting the function "x*x*x*x" (which it bytecompiles to three
multiplications) is faster than "x^4" (which it bytecompiles to one
std::pow() call). std::pow() can be incredibly slow.