Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tested the performance difference between <code>x*x*...</code> vs <code>pow(x,i)</code> for small <code>i</code> using this code:</p> <pre><code>#include &lt;cstdlib&gt; #include &lt;cmath&gt; #include &lt;boost/date_time/posix_time/posix_time.hpp&gt; inline boost::posix_time::ptime now() { return boost::posix_time::microsec_clock::local_time(); } #define TEST(num, expression) \ double test##num(double b, long loops) \ { \ double x = 0.0; \ \ boost::posix_time::ptime startTime = now(); \ for (long i=0; i&lt;loops; ++i) \ { \ x += expression; \ x += expression; \ x += expression; \ x += expression; \ x += expression; \ x += expression; \ x += expression; \ x += expression; \ x += expression; \ x += expression; \ } \ boost::posix_time::time_duration elapsed = now() - startTime; \ \ std::cout &lt;&lt; elapsed &lt;&lt; " "; \ \ return x; \ } TEST(1, b) TEST(2, b*b) TEST(3, b*b*b) TEST(4, b*b*b*b) TEST(5, b*b*b*b*b) template &lt;int exponent&gt; double testpow(double base, long loops) { double x = 0.0; boost::posix_time::ptime startTime = now(); for (long i=0; i&lt;loops; ++i) { x += std::pow(base, exponent); x += std::pow(base, exponent); x += std::pow(base, exponent); x += std::pow(base, exponent); x += std::pow(base, exponent); x += std::pow(base, exponent); x += std::pow(base, exponent); x += std::pow(base, exponent); x += std::pow(base, exponent); x += std::pow(base, exponent); } boost::posix_time::time_duration elapsed = now() - startTime; std::cout &lt;&lt; elapsed &lt;&lt; " "; return x; } int main() { using std::cout; long loops = 100000000l; double x = 0.0; cout &lt;&lt; "1 "; x += testpow&lt;1&gt;(rand(), loops); x += test1(rand(), loops); cout &lt;&lt; "\n2 "; x += testpow&lt;2&gt;(rand(), loops); x += test2(rand(), loops); cout &lt;&lt; "\n3 "; x += testpow&lt;3&gt;(rand(), loops); x += test3(rand(), loops); cout &lt;&lt; "\n4 "; x += testpow&lt;4&gt;(rand(), loops); x += test4(rand(), loops); cout &lt;&lt; "\n5 "; x += testpow&lt;5&gt;(rand(), loops); x += test5(rand(), loops); cout &lt;&lt; "\n" &lt;&lt; x &lt;&lt; "\n"; } </code></pre> <p>Results are:</p> <pre><code>1 00:00:01.126008 00:00:01.128338 2 00:00:01.125832 00:00:01.127227 3 00:00:01.125563 00:00:01.126590 4 00:00:01.126289 00:00:01.126086 5 00:00:01.126570 00:00:01.125930 2.45829e+54 </code></pre> <p>Note that I accumulate the result of every pow calculation to make sure the compiler doesn't optimize it away.</p> <p>If I use the <code>std::pow(double, double)</code> version, and <code>loops = 1000000l</code>, I get:</p> <pre><code>1 00:00:00.011339 00:00:00.011262 2 00:00:00.011259 00:00:00.011254 3 00:00:00.975658 00:00:00.011254 4 00:00:00.976427 00:00:00.011254 5 00:00:00.973029 00:00:00.011254 2.45829e+52 </code></pre> <p>This is on an Intel Core Duo running Ubuntu 9.10 64bit. Compiled using gcc 4.4.1 with -o2 optimization.</p> <p>So in C, yes <code>x*x*x</code> will be faster than <code>pow(x, 3)</code>, because there is no <code>pow(double, int)</code> overload. In C++, it will be the roughly same. (Assuming the methodology in my testing is correct.)</p> <hr> <p>This is in response to the comment made by An Markm:</p> <p>Even if a <code>using namespace std</code> directive was issued, if the second parameter to <code>pow</code> is an <code>int</code>, then the <code>std::pow(double, int)</code> overload from <code>&lt;cmath&gt;</code> will be called instead of <code>::pow(double, double)</code> from <code>&lt;math.h&gt;</code>.</p> <p>This test code confirms that behavior:</p> <pre><code>#include &lt;iostream&gt; namespace foo { double bar(double x, int i) { std::cout &lt;&lt; "foo::bar\n"; return x*i; } } double bar(double x, double y) { std::cout &lt;&lt; "::bar\n"; return x*y; } using namespace foo; int main() { double a = bar(1.2, 3); // Prints "foo::bar" std::cout &lt;&lt; a &lt;&lt; "\n"; return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload