Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here we face 2 blocks of code.</p> <ul> <li>A large block of code that is designed to take an array and work through it. </li> <li>A piece of code which uses previous block of code with some data.</li> </ul> <p><strong>Structure your code.</strong></p> <p>The large block of code should be a function possibly divided in several subfunctions. The other piece of code will call to this function.</p> <p><strong>Parameters of the function. Array or single char.</strong></p> <pre><code> (a) void work( char c ); (b) void work( char&amp; c ); (c) void work( const char v[], size_t size); (d) void work( char v[], size_t size); </code></pre> <p>Options (a) and (b) should be used if the kind of work makes no sense for an array. Which is not the case.<br/> Options (c) and (d) should be used if work makes sense for an array.</p> <p>So use an array.</p> <p><strong>Variable which holds the data. Array or single char.</strong></p> <p>If you only need to hold a single char then use a single non-array char. You can still call the array function.</p> <pre><code>char c; c = 'b'; work( &amp;c, 1 ); ////// char a[1]; a[0] = 'b'; work( a, 1 ); </code></pre> <p>The work function sees the single variable and the array as an array of size 1. Code will work fine in both cases and with no efficiency concerns.</p> <p><strong>Testing</strong></p> <p>Let's see if real code holds my previous statements.</p> <pre><code>#include &lt;iostream&gt; #include &lt;ctime&gt; #include &lt;vector&gt; #include &lt;cstddef&gt; #include &lt;chrono&gt; using namespace std; unsigned long miliTime() { return std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1); } // An hypotetical work function with arrays void workArray( char v[], size_t size ) { for ( size_t n=0; n&lt;size; ++n ) { // large block of code for ( int i=0; i&lt;1000; ++i ) { v[n] += 3 + i; if (v[n] == '3' ) v[n] = 'J' - v[n]; v[n] = toupper( v[n] ) + '-'; } } } // Same function just for a single character void workSingle( char&amp; c ) { // large block of code for ( int i=0; i&lt;1000; ++i ) { c += 3 + i; if (c == '3' ) c = 'J' - c; c = toupper( c ) + '-'; } } int main(void) { const long int repeats =1000000; long int n; unsigned long start; double dif; start = miliTime(); char c; c = 'b'; for ( n=0; n&lt;repeats; ++n) workArray( &amp;c, 1 ); dif = miliTime() - start; cout &lt;&lt; "Result = " &lt;&lt; c &lt;&lt; endl; cout &lt;&lt; "Non-array var passed to array code = " &lt;&lt; dif &lt;&lt; " ms" &lt;&lt; endl; start = miliTime(); char a[1]; a[0] = 'b'; for ( n=0; n&lt;repeats; ++n) workArray( a, 1 ); dif = miliTime() - start; cout &lt;&lt; "Result = " &lt;&lt; a[0] &lt;&lt; endl; cout &lt;&lt; "Array var passed to array code = " &lt;&lt; dif &lt;&lt; "ms" &lt;&lt; endl; start = miliTime(); char c2; c2 = 'b'; for ( n=0; n&lt;repeats; ++n) workSingle( c2 ); dif = miliTime() - start; cout &lt;&lt; "Result = " &lt;&lt; c2 &lt;&lt; endl; cout &lt;&lt; "Non-array var passed to non-array code = " &lt;&lt; dif &lt;&lt; "ms" &lt;&lt; endl; start = miliTime(); char a2[1]; a2[0] = 'b'; for ( n=0; n&lt;repeats; ++n) workSingle( a2[0] ); dif = miliTime() - start; cout &lt;&lt; "Result = " &lt;&lt; a2[0] &lt;&lt; endl; cout &lt;&lt; "Array var passed to non-array code = " &lt;&lt; dif &lt;&lt; "ms" &lt;&lt; endl; } </code></pre> <p>When compiled under gcc-4.7 with this command line and executed in my computer :</p> <pre><code>g++ -O2 -Wall -std=c++11 x.cpp -o x.out &amp;&amp; ./x.out </code></pre> <p>I get this output :<br/> Result = z<br/> Non-array var passed to array code = 5520 ms<br/> Result = z<br/> Array var passed to array code = 5515ms<br/> Result = z<br/> Non-array var passed to non-array code = 5203ms<br/> Result = z<br/> Array var passed to non-array code = 5203ms<br/></p> <p>As expected the result is always the same. There is no significative difference in passing an array or a non-array variable to the work function for both implementations.<br/></p> <p>workSingle is 6% faster than workArray. <br/> The execution of the outer loop (which does not exist in workSingle) is unlikely to be the cause since the inner loop executes 1000 times. The cause is probably due to access to v[n] being slower than access to c due to the indirection.<br/> Though if you change the 1000 in the inner loop for a global variable read from std::cin then workSingle actually gives slower times than workArray!</p> <p>Some kind of optimizations, cache misses or other low level stuff may be the cause. I would not sacrifice the reusability of workArray for the uncertain efficiency of workSingle unless time is so critical that you are willing to go to assembly level.</p> <p><strong>Conclusion.</strong></p> <p>Declare your variable as non-array since it only has to hold a single character.<br/> Implement your large section of code as a function that takes an array parameter. Possibly divided in several subsections if its so large.</p>
    singulars
    1. This table or related slice is empty.
    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. 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