is Matlab code a lot faster than Excel formulas?
Show older comments
I have a very large Excel spreadsheet that I use to do an extensive calculation with many iterations. The calculation is done by spreadhseet formulas, and the iteration is done by a VBA macro.
The spreadsheet is 1000 columns X 10,000 rows, and one iteration takes about 30 secs. A typical calculation "run" has 30,000 iterations, so total calculation time is ~ 10 days
I'd like to speed this up by coding the spreadsheet in Matlab. I read it is up to 100 X faster than Excel, although to make it worth it to me, 10 X would be enough.
Given the above, is a 10 X speed increase a reasonable expectation when converting Excel formulas to Matlab code?
I'm a new Matlab user, and very grateful for your input. :-)
6 Comments
dpb
on 10 Jan 2023
It will depend greatly on how the code is written, but in general one would expect significantly faster execution with computation code. If the model can be extracted from the spreadsheet, using just the data as input and then writing its output when computed, I'd venture quite probably will see a significant improvement.
This will, of course, require that you're not going to rely on builtin solver for other similar toolsets.
John D'Errico
on 10 Jan 2023
Any code can be fast or slow. It all depends on how well the code was written, on how well the programmer understands the numerical methods in volved. And sometimes there are better ways to do any computation.
Can we know what you did in the spreadsheet, and why it is so slow? OF COURSE NOT! Sadly, the odds are good that if your knowledge of MATLAB is poor and your skills at numerical computing are poor, then the MATLAB code you would write might just mimic exactly what you did in Excel. And then, it will still probably be slow. Sorry. But this is only a guess. The iterative solver used by you in Excel may just be a poor way of solving the problem. There may be better tools in MATLAB to solve the problem, or possibly not.
For example, I can give the example of a coupled system of nonlinear PDEs I solved long ago in Excel. It took a seriously long time to converge (at least hours, I recall.) But that was me, lacking any real expertise in solving those problems. That is not to say I am an expert in the subject now, but I can be positive that my solution now would be far better, done in MATLAB. Of this I am quite confident.
Anyway, what you don't want to do is to just code the spreadsheet in MATLAB. You won't be happy, and then you will give up, thinking MATLAB is no better than Excel. Solve the problem, using the tools in MATLAB. Don't just execute code that mimics a spreadsheet.
And of course, we don't even know what the problem you are trying to solve. So all of this is just wild speculation.
Jim Riggs
on 10 Jan 2023
If you have code that takes ~10 days to run in Excel and you need to repeat this or update the result many times, then you realy should be using a compiled code. I have performed benchmarks in the past comparing Fortran with Matlab, and the identical implementation runs 500 to 1000 times faster in compiled Fortran vs. Matlab. (And Python is another factor of 6 slower than Matlab.)
Matlab has a compiler which creates standalone executable code - this would be one solution to getting that exponential improvement in speed. Or you might consider coding it in C/C++.
A 500x improvement would get that 10 days down to under 1/2 hour. But if Excel macros are more like Python, you might get 3000x to 6000x improvement using compiled code. That would get you to 2.5 to 5 minutes. It's definitely worth exploring.
John D'Errico
on 10 Jan 2023
While Jim is correct, in that code can be compiled to improve it, I'd point out that it may be missing an important fact. For example, suppose the excel solver is simply solving a large linear system of equations, using iterative methods. Great, except that is something MATLAB will do VERY well. And the code is already compiled. So you gain nothing by use of compiled code there. What really matters is the algorithmic improvement, of using a better method to solve the problem.
The issue is, we know nothing about what is being solved. Is this a problem that is amenable to the use of a better tool to solve an easily solved problem? Until then, there is no way to offer good advice.
Berliner JFK
on 11 Jan 2023
Accepted Answer
More Answers (1)
Bruno Luong
on 11 Jan 2023
Edited: Bruno Luong
on 11 Jan 2023
Impossible to answer with such fuzzy question; the time depends on your hardware, your matlab version, your licences to acess to some toolbox suh as parallel computing, your data, your algorithm, your MATAB skill, ...
Here is the time of some arbitrary formula run on TMW server.
A=randn(1000,10000);
P = [1 2 3];
f = @(x) sin(polyval(P,x)).*(x>=0);
tic
f(A);
toc
2 Comments
Berliner JFK
on 11 Jan 2023
Bruno Luong
on 11 Jan 2023
I suggest you to test the same typical but possibly simple formula you would use on Excel and MATLAB to get your own idea.
I could be 100 faster, it could be the same speed, it could be slower. So far there is not enough details in your question that can be answered with certainty.
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!