MATLAB executable is too slow
Asked Answered
E

4

23

I converted my MATLAB program into a console-based application using the deploytool in MATLAB. The MATLAB .m file takes around 2 seconds to execute, but after I converted it into an executable and called the .exe, it takes 45 seconds to execute which is too long.

I want to integrate the MATLAB program with PHP. Is there another efficient and fast way to do this? In my project, time is really a big factor (not the developing time but the execution time of the application). So is there a method that takes less time?

I saw on the Internet that we can write PHP extensions to call the MATLAB. Is this method fast or the same as calling the .exe file? Is a MATLAB coder any help for this process? If there is an alternative option, please mention it.

Entoblast answered 12/4, 2013 at 9:2 Comment(8)
One thing you may want to do is track the actual calculation time. At least you can then pinpoint whether the call is slower or the calculation itself.Nevels
how can I track that? I used the run and time but it just gives the total time.Entoblast
the matlab .m file just takes 2 secs to execute but the exe takes as I mention 45 secs so is it the call that is slow?Entoblast
Probably, but not necessarily as there may be different inputs/conditions. I usually use tic and toc to record timing differences.Nevels
Yes the time execution of code within the exe and the .m file is slightly different.For the .m file:Elapsed time is 1.661620 seconds.Elapsed time is 0.004483 seconds.for two process. For the exe file Elapsed time is 2.610610 seconds. Elapsed time is 0.021953 seconds.for the same two process.But the total execution time of the exe is 40sec so I think it is taking the time to call the exe. What can be the solution for this?Entoblast
Could you try two things? - Deactivate the firewall - Run the .exe as administrator (right click on the icon)Bolingbroke
Which MATLAB version are you using?Elevenses
Can anyone explain to me how a user with 49 reputation can offer a 50 point bounty?Pomcroy
E
16

A MATLAB compiled .exe will suffer from overhead at the first time you run it becuase it is starting the MCR: Why does my application compiled with the MATLAB Compiler 4.1 take a long time to start up?

Unless you log off or restart your OS, the MCR will remain pre-loaded. Another useful read: Speeding up compiled apps startup.

"Why does my stand-alone created using the MATLAB Compiler take longer to start the first time?" also reports that consecutive runs should be faster, but if you rerun later, you will have to reload the process in memory.

You can enclose your code within tic toc, deploy it and check how much time the execution is taking, against startup overhead.

The alternative to speeding up the .exe would be to call MATLAB with PHP. If you keep the MATLAB session open you run into the overhead once. You could launch MATLAB at startup, thus avoiding to suffer the overhead specifically during the call with PHP.

For more info read Calling MATLAB from PHP, and keep in mind that you don't want to use exit unless specifically needed.

Elevenses answered 12/4, 2013 at 9:21 Comment(8)
A best practice for writing compiled applications is to develop them so that they can run without being restarted often. That is, you can try to design your application so that it does not exit unless absolutely necessary. This way the MCR will not need to be initialized often. But how can I achieve this?Entoblast
Unless you log off or restart your OS, the MCR will remain pre-loaded. Another useful read: Speeding up compiled apps startupElevenses
I tried executing the exe file continously but no change in timeEntoblast
Though the content of your answer is correct, I don't think it answers the question.Nevels
@Entoblast have you tried to time the execution of the code within the .exe with a tic toc?Elevenses
Yes the time execution of code within the exe and the .m file is slightly different.For the .m file:Elapsed time is 1.661620 seconds.Elapsed time is 0.004483 seconds.for two process. For the exe file Elapsed time is 2.610610 seconds. Elapsed time is 0.021953 seconds.for the same two process.But the total execution time of the exe is 40sec so I think it is taking the time to call the exe. What can be the solution for this?Entoblast
I assume you got no improvements by setting the MCR_CACHE_ROOT (link in my previous comment)? The technical solution "Why does my stand-alone created using the MATLAB Compiler take longer to start the first time?" also reports that consecutive runs should be faster, but if you rerun later, you will have to reload the process in memory.Elevenses
The link for Calling MATLAB from PHP is deadNevels
S
7

Calling an executable created with MATLAB Compiler will suffer an overhead relative to calling the program within live MATLAB, as it needs to start the MCR. This will be longer the first time you start it, but there will still be an overhead even after the first time.

If you have access not only to MATLAB Compiler, but to one of the Builder products (Builder for .NET or - which is probably better since you're using PHP - Builder for Java) there is a way of working around this.

Using the Builder products you can create a standalone component (either a .NET assembly or a Java .jar). You can then create a .NET or Java application that will run, instantiate your MATLAB-built component - which starts the MCR - and then sit there and wait for a call from your PHP. Each call will then not suffer the MCR startup overhead at all, and should only have a much smaller overhead from the call from PHP to .NET/Java.

Shugart answered 17/4, 2013 at 12:7 Comment(6)
Thanks Sam for the answer.Yes, I have tried this too. But can we convert the .m file to mex file will this process be faster or the same?If you have any idea about conversion from .m to mex files please suggest.Entoblast
See my answer to your other question.Shugart
Will excluding some toolbox while creating a .Net assembly speed up the process abit?I made a console based C# application and called the exe from php any other methods to do?Entoblast
Only a little bit - it will potentially make the assembly smaller, and it will build the assembly faster, but it won't run a great deal faster.Shugart
how can I find out which are the toolbox that are dependent on my program. I tried generating the dependency report but cannot figure out any other method?Entoblast
Other than just knowing because you wrote it, or by combing through and manually searching for all dependencies, the Dependency Report is the right method. You can accomplish the same thing with the command depfun.Shugart
W
2

As others said, when you call the executables, there a (really big for matlab) overhead when the executable is loaded. The solution? Load it only one time. How to do it? It's complicated and depend on the platforms.

I think that the most portable way to do somethings like it is to make a server in matlab (I know this is possible, but I never tried). So when you need to use it, you simply connect to it and send the arguments.

Take a look at this: HTTP server in matlab and this: http://blogs.mathworks.com/loren/2011/05/27/transferring-data-between-two-computers-using-matlab/

Willetta answered 23/4, 2013 at 1:40 Comment(0)
C
1

Just as an addition to everything said above, may I suggest setting up a RAM drive, and setting MCR_CACHE_ROOT system variable to a directory on a RAM drive.

This will significantly improve further startup times.

Make sure your RAM drive is persistent during reboots, if anyone uses this scheme on a desktop.

Caliphate answered 2/4, 2020 at 10:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.