# how to make the execution of a Matlab program faster?

Are there any techniques to make MATLAB code execute faster?

I am talking about things that can make my PC faster (like stop some .exe or software to improve PC performance). I noticed that sometimes my PC running the same task in two different times: for example, he can run a program in 45 seconds, and the same program again in 10 minutes.

-

## migrated from stackoverflow.comJun 21 '10 at 2:03

This question came from our site for professional and enthusiast programmers.

It is not really a MATLAB-related question (although on some earlier R14 versions antivirus could make it run slow). As a rule of thumb: to run fast you have to do less. – Mikhail Jun 19 '10 at 22:19

Is it MATLAB that is running slow on your computer or rather the programs you've written in MATLAB?

MATLAB makes heavy use of dynamic memory allocation and reallocation; MAtrix or array resizing can be very slow and non-deterministic; if a matrix or array grows, new memory is allocated and the existing data copied to the new memory before deleting the old copy. You can avoid this memory thrashing by pre-allocating such arrays: for example this code:

``````x = 0;
for k = 2:1000
x(k) = x(k-1) + 5;
end
``````

causes `x` to be resized on each iteration and will run very slow. By comparison the following code:

``````x = zeros(1, 1000);
for k = 2:1000
x(k) = x(k-1) + 5;
end
``````

pre-allocates the array so that no resizing occurs and will run considerably faster.

The MathWorks website has a whole page detailing this and other performance improving techniques; the page covers the following:

• Preallocating Arrays
• Use Distributed Arrays for Large Datasets
• When Possible, Replace for with parfor (Parallel for)
• Limiting Size and Complexity
• Assigning to Variables
• Using Appropriate Logical Operators
• Functions Are Generally Faster Than Scripts
• Load and Save Are Faster Than File I/O Functions
• Vectorizing Loops
• Avoid Large Background Processes

Note that the solution you have suggested is last on MathWorks' list, you should look at all the other points first, unless you are doing something obviously resource hungry like running a Virtual Machine, or playing high definition video at the same time as running MATLAB, looking at other processes running is probably sweating the small stuff.

-
Thank you for your answer However, I think the problem is not related with the Matlab code, since the same program is executed in two different times (45 seconds and 10 minutes). Maybe the forum is reserved for programming, but my problem shows that the slow execution of a Matlab program can be other than a bad script. Therefore, before conducting a simulation, we must first verify that the PC is working properly and it is also important for a programmer that the content of its program. – Anonymous Jun 20 '10 at 15:29
@bzak: In that case I add my vote to close, although non-deterministic behaviour means exactly what you are seeing. If the algorithm at some point allocates sufficient memory to require swapping memory to disk, the slow down will be significant. In MATLAB, adding memory is often the most effective accelerator. – Clifford Jun 20 '10 at 17:20

After following Clifford suggestions, you can use the MATLAB profiling tools to check where your program spends most of its time, usually not too many lines/functions. Then focus on optimizing those lines.

-

According to me it must be some system-wide resource bottleneck like disk access or memory swapping issue. Try to see what comes up in Process Explorer, there you might find out what is going on in the system.

-
Almost certainly what is going on is MATLAB. It is notoriously resource hungry. Both you and bzak are probably barking up the wrong tree if you think the solution is to fix the PC rather than the MATLAB code. Moreover such action is not a subject for this forum. – Clifford Jun 19 '10 at 22:02

If the running time for your code is highly variable, then it is likely that you have a problem where sometimes, to some extent, you are verging into virtual memory. When MATLAB must use swap space, things slow down dramatically. After all, you are using disk space to substitute for the availability of physical RAM. This is the best reason I can offer for a code that varies in time by an order of magnitude in the time taken for different runs.

Use the profiler tool to determine where the time is going. Look carefully at those lines. Are you creating a large array, even if that is done temporarily in some function you call?

Check your system. Look to see if when this happens, if the CPU is running essentially idle while disk accesses are heavy.

Of course, if this is as I suggest, then the solution is logical too.

• Make your algorithm more efficient in terms of memory. This is ALWAYS a good thing.

• Get more RAM if you currently have less than 2GB.

• Use the 64 bit version of MATLAB along with getting more RAM.

-
+1. If there's no I/O problem, this is most certainly the reason. Once you start paging, everything slows to a crawl. – Jonas Jun 20 '10 at 16:29