VBA vs C++ function – which one is faster in Excel?

Comparing the speed of VBA and C++ in general is quite not fair. Its like comparing the speed of a military jet and a Boeing 767. But still, it’s interesting to see how much C++ is faster in Excel’s native world – the Visual Basic Editor.


So, I have decided to build a simple Fibonacci with recursion function in both languages and simply to compare its speed with Excel. In general. Fibonacci with recursion is something that you should NEVER do at work or in a production code, because it is extremely slow, due to the huge number of useless calculations. See here for the memoization optimization.

So, as noted, I have used Fibonacci with Recursion, because it’s really easy to simulate a few billion useless calculations, thus the difference would be noted by everyone easily.

In C++ this is our function:

And the *.def file, needed to export the *.dll library:

In VBA this is our function:

As you see, the functions are exactly the same and they make billions of calculation, due to the useless recursion and lack of memoization. In my tests, I have noted that up to using 10 a number of times for calculating the Fibonacci, both C++ and VBA work quite fast (no doubt about it, we do not reach a billion calculations there). However, if we increase the number of tests to 40, the result is really not in a favour of VBA. The speed of C++ is more than twice faster:

Yup, C++ really beats VBA in its own realm –  the VB Editor. And the bad news for VBA do not end here –  C++ is not just faster, it is exponentially faster, concerning the fact that at the beginning the results are quite the same.

That’s the VBA whole code I have used:


Still, VBA is fast enough! 🙂

Tagged with: , , ,