Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The question comes down to "does the compiler know the answer at compile time"?</p> <p>In the case you gave, the answer is "no". Since it is possible to change the size of the vector inside the loop it should get evaluated every time (but it's pretty fast since it's not counting one at a time).</p> <p>Code like this:</p> <pre><code>int myArray[20]; for(int ii = 0; ii &lt; sizeof(myArray)/sizeof(*myArray); ii++) { </code></pre> <p>will in fact evaluate <code>sizeof</code> at compile time, and be very marginally more efficient because of this. Note - you need the whole expression since <code>sizeof(myArray)</code> by itself will return <code>80</code> in this case (where an <code>int</code> is four bytes). The advantage of using <code>sizeof(*myArray)</code> is that if you change your mind about the type of <code>myArray</code>, this line of code doesn't break…</p> <p>And the fact that you are using a division here doesn't slow down execution, since it's all done during compile time (only once).</p> <p>To re-iterate what I said in my comment: in this instance, the version you are proposing, whilst evaluating the size of the vector on each iteration, is really not a problem. It is extremely unlikely that you could see a performance difference in all but the tightest loops. Don't fall into <a href="http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html" rel="nofollow">the trap of micro-optimization</a></p> <p>Here is a simple timing example:</p> <pre><code>#include &lt;iostream&gt; #include &lt;ctime&gt; #include &lt;vector&gt; using namespace std; int main(void) { vector&lt;int&gt; testVector(200); int ii, jj; register int ss; time_t startT, endT; // case 1: using a constant for loop condition startT = clock(); for(ii = 0; ii &lt; 100000; ii++) { for(jj = 0; jj &lt; 200; jj++) { testVector[jj] = ii - jj; } } endT = clock(); printf("using constant: elapsed time: %.2f ms\n", (endT - startT) * 1000.0 / CLOCKS_PER_SEC); // case 2: using size(): startT = clock(); for(ii = 0; ii &lt; 100000; ii++) { for(jj = 0; jj &lt; testVector.size(); jj++) { testVector[jj] = ii - jj; } } endT = clock(); printf("using size: elapsed time: %.2f ms\n", (endT - startT) * 1000.0 / CLOCKS_PER_SEC); // case 3: single call to size(): startT = clock(); ss = testVector.size(); for(ii = 0; ii &lt; 100000; ii++) { for(jj = 0; jj &lt; ss; jj++) { testVector[jj] = ii - jj; } } endT = clock(); printf("with size out of loop: elapsed time: %.2f ms\n", (endT - startT) * 1000.0 / CLOCKS_PER_SEC); } </code></pre> <p>This gives the following result:</p> <pre><code>using constant: elapsed time: 162.47 ms using size: elapsed time: 277.02 ms with size out of loop: elapsed time: 241.01 ms </code></pre> <p>As you can see, there <em>is</em> a finite amount of time associated with looking up the size of the vector in every loop; but it is about 100 ms for 20,000,000 calls, or 5 ns per call. That sounds about right; as you can see, when your loop is tight enough it is measurable; but it is unlikely to be of practical significance in most "real" code instances (where you are likely to do more in your loop). As you can also see, just moving the call to <code>size</code> out of the loop doest help much - using a constant vs a variable is a bigger difference. </p>
 

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