Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is std::sin() and std::cos() slower than sin() and cos()?
    primarykey
    data
    text
    <p>Test code:</p> <pre><code>#include &lt;cmath&gt; #include &lt;cstdio&gt; const int N = 4096; const float PI = 3.1415926535897932384626; float cosine[N][N]; float sine[N][N]; int main() { printf("a\n"); for (int i = 0; i &lt; N; i++) { for (int j = 0; j &lt; N; j++) { cosine[i][j] = cos(i*j*2*PI/N); sine[i][j] = sin(-i*j*2*PI/N); } } printf("b\n"); } </code></pre> <p>Here is the time:</p> <pre><code>$ g++ main.cc -o main $ time ./main a b real 0m1.406s user 0m1.370s sys 0m0.030s </code></pre> <p>After adding <code>using namespace std;</code>, the time is:</p> <pre><code>$ g++ main.cc -o main $ time ./main a b real 0m8.743s user 0m8.680s sys 0m0.030s </code></pre> <p>Compiler:</p> <pre><code>$ g++ --version g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 </code></pre> <p>Assembly:</p> <pre><code>Dump of assembler code for function sin@plt: 0x0000000000400500 &lt;+0&gt;: jmpq *0x200b12(%rip) # 0x601018 &lt;_GLOBAL_OFFSET_TABLE_+48&gt; 0x0000000000400506 &lt;+6&gt;: pushq $0x3 0x000000000040050b &lt;+11&gt;: jmpq 0x4004c0 End of assembler dump. Dump of assembler code for function std::sin(float): 0x0000000000400702 &lt;+0&gt;: push %rbp 0x0000000000400703 &lt;+1&gt;: mov %rsp,%rbp 0x0000000000400706 &lt;+4&gt;: sub $0x10,%rsp 0x000000000040070a &lt;+8&gt;: movss %xmm0,-0x4(%rbp) 0x000000000040070f &lt;+13&gt;: movss -0x4(%rbp),%xmm0 0x0000000000400714 &lt;+18&gt;: callq 0x400500 &lt;sinf@plt&gt; 0x0000000000400719 &lt;+23&gt;: leaveq 0x000000000040071a &lt;+24&gt;: retq End of assembler dump. Dump of assembler code for function sinf@plt: 0x0000000000400500 &lt;+0&gt;: jmpq *0x200b12(%rip) # 0x601018 &lt;_GLOBAL_OFFSET_TABLE_+48&gt; 0x0000000000400506 &lt;+6&gt;: pushq $0x3 0x000000000040050b &lt;+11&gt;: jmpq 0x4004c0 End of assembler dump. </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.
 

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