Note that there are some explanatory texts on larger screens.

plurals
  1. POC++11 tuple performance
    text
    copied!<p>I just about to make my code more generalized by using <code>std::tuple</code> in a lot of cases including single element. I mean for example <code>tuple&lt;double&gt;</code> instead of <code>double</code>. But I decided to check performance of this particular case.</p> <p>Here is simple performance benchmark test:</p> <pre><code>#include &lt;tuple&gt; #include &lt;iostream&gt; using std::cout; using std::endl; using std::get; using std::tuple; int main(void) { #ifdef TUPLE using double_t = std::tuple&lt;double&gt;; #else using double_t = double; #endif constexpr int count = 1e9; auto array = new double_t[count]; long long sum = 0; for (int idx = 0; idx &lt; count; ++idx) { #ifdef TUPLE sum += get&lt;0&gt;(array[idx]); #else sum += array[idx]; #endif } delete[] array; cout &lt;&lt; sum &lt;&lt; endl; // just "external" side effect for variable sum. } </code></pre> <p>And run results:</p> <pre><code>$ g++ -DTUPLE -O2 -std=c++11 test.cpp &amp;&amp; time ./a.out 0 real 0m3.347s user 0m2.839s sys 0m0.485s $ g++ -O2 -std=c++11 test.cpp &amp;&amp; time ./a.out 0 real 0m2.963s user 0m2.424s sys 0m0.519s </code></pre> <p>I thought that tuple is strict static-compiled template and all of get&lt;> functions are working just usual variable access in that case. BTW memory allocation sizes in this test are same. Why does this execution time difference happens?</p> <p>EDIT: Problem was in initialization of tuple&lt;> object. To make test more accurate one line must be changed:</p> <pre><code> constexpr int count = 1e9; - auto array = new double_t[count]; + auto array = new double_t[count](); long long sum = 0; </code></pre> <p>After that one can observe similar results:</p> <pre><code>$ g++ -DTUPLE -g -O2 -std=c++11 test.cpp &amp;&amp; (for i in $(seq 3); do time ./a.out; done) 2&gt;&amp;1 | grep real real 0m3.342s real 0m3.339s real 0m3.343s $ g++ -g -O2 -std=c++11 test.cpp &amp;&amp; (for i in $(seq 3); do time ./a.out; done) 2&gt;&amp;1 | grep real real 0m3.349s real 0m3.339s real 0m3.334s </code></pre>
 

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