Square a float: pow or f*f?

Discussion in 'C++' started by chrisstankevitz@gmail.com, Jun 22, 2008.

  1. Guest

    Any on consensus on which of these is faster?

    inline float Square1(float f) { return std::pow(f, 2.0f); }
    inline float Square2(float f) { return f*f; }

    Chris
     
    , Jun 22, 2008
    #1
    1. Advertising

  2. Guest

    On Jun 21, 5:41 pm, "Alf P. Steinbach" <> wrote:
    > For optimizations, follow the accepted principles (1) don't do it, (2) don't do
    > it yet, and (3) if you still feel an irresistible urge, then measure, measure,
    > measure, and finally, don't do it.


    Alf,

    Thanks for the response. I am reorganizing a function that, according
    to my profiler, is the bottleneck. I suppose the answer is I need to
    try both methods and see which is faster according to the profiler. I
    was hoping someone here would know for the special case of squaring.

    Thanks again for your help,

    Chris
     
    , Jun 22, 2008
    #2
    1. Advertising

  3. James Kanze Guest

    On Jun 22, 3:07 am, wrote:
    > On Jun 21, 5:41 pm, "Alf P. Steinbach" <> wrote:


    > > For optimizations, follow the accepted principles (1) don't
    > > do it, (2) don't do it yet, and (3) if you still feel an
    > > irresistible urge, then measure, measure, measure, and
    > > finally, don't do it.


    > Thanks for the response. I am reorganizing a function that,
    > according to my profiler, is the bottleneck. I suppose the
    > answer is I need to try both methods and see which is faster
    > according to the profiler. I was hoping someone here would
    > know for the special case of squaring.


    The answer is that it will depend on the machine and the
    compiler (although typically, I would expect a*a to be faster).
    The answer is also that even in a tight loop which does nothing
    else, the difference is likely to be insignificant.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 22, 2008
    #3
  4. James Kanze wrote:
    > The answer is that it will depend on the machine and the
    > compiler (although typically, I would expect a*a to be faster).
    > The answer is also that even in a tight loop which does nothing
    > else, the difference is likely to be insignificant.


    At least on Intel processors a pow() will be inherently slower than a
    multiplication. However, many compilers are able to optimize a
    "std::pow(d, 2.0)" call into "d*d".

    I tested this on my computer, using the program below, using gcc 4.1.2
    with the compiler options "-O3 -march=pentium4 -s" and I got these results:

    Time: 1.69 s, result = 2.66667e+18
    Time: 1.69 s, result = 2.66667e+18
    Time: 47.72 s, result = 2.69852e+18

    The first and second tests show no difference, so clearly gcc is
    optimizing the pow() call away. The third version forces gcc to perform
    a true pow() call, and it's a lot slower.


    #include <cmath>
    #include <ctime>
    #include <iostream>

    inline double square1(double d) { return d*d; }
    inline double square2(double d) { return std::pow(d, 2.0); }
    inline double square3(double d) { return std::pow(d, 2.001); }

    template<typename F>
    void test(F f)
    {
    clock_t t1 = std::clock();
    double res = 0, d = .001;
    for(int i = 0; i < 200000000; ++i)
    {
    res += f(d);
    d += .001;
    }
    clock_t t2 = std::clock();

    std::cout << "Time: " << int((t2-t1)*100.0/CLOCKS_PER_SEC)/100.0
    << " s, result = " << res << std::endl;
    }

    int main()
    {
    test(square1);
    test(square2);
    test(square3);
    }
     
    Juha Nieminen, Jun 22, 2008
    #4
  5. Guest

    On Jun 22, 8:45 am, Juha Nieminen <> wrote:
    > The first and second tests show no difference, so clearly gcc is
    > optimizing the pow() call away. The third version forces gcc to perform
    > a true pow() call, and it's a lot slower.
    >


    Juha,

    Thanks for your help and test results!

    Chris
     
    , Jun 22, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,266
  2. bd
    Replies:
    0
    Views:
    660
  3. Clueless Moron

    math.pow vs pow

    Clueless Moron, Nov 27, 2003, in forum: Python
    Replies:
    5
    Views:
    957
    John J. Lee
    Nov 28, 2003
  4. Michel Rouzic

    pow(2, 1/2) != pow(2, 0.5) problem

    Michel Rouzic, Jun 15, 2005, in forum: C Programming
    Replies:
    52
    Views:
    1,729
    Alan Balmer
    Jun 20, 2005
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,628
    James Kanze
    Oct 8, 2009
Loading...

Share This Page