Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your benchmark is off, the margin should be way larger.</p> <p>For instance, on my computer, the C++ code takes 7.46s (compiled without optimisations, timed with the Unix <code>time</code> tool); the VBA code by contrast took 46s. This is a much more realistic margin (but I wouldn’t have been surprised by an even larger one – conventional, non-optimising interpreters are usually at least a factor ten slower).</p> <p>For one thing, VBA first has to read in the code and either interpret it line by line or transform it into an intermediate representation. Nevertheless, this intermediate code representation is still <em>interpreted</em>, not run directly by the CPU (or did you count the compilation time in C++?).<sup>1</sup></p> <p>In addition, VB(A) does a lot of redundant stuff in this code which is absent in C++: for instance, every single integer addition (which is implicit in your code in the incrementation of the <code>For</code> loop variable) is guarded against overflow. Essentially, this VB code</p> <pre><code>i = i + 1 </code></pre> <p>is transformed to the following pseudo intermediate code:</p> <pre><code>if i + 1 overflows then raise an overflow error end if i = i + 1 </code></pre> <p>… for <em>every</em> integer operation. That consumes quite a lot of runtime. The same goes for array (or here: cell) access operations.</p> <hr> <p><sup>1</sup> I <em>think</em> VBA uses an intermediate representation called <em>P-code</em> rather than line by line code interpretation.</p>
    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