I recently built a computer model for the Soave-Redlich-Kwong (SRK) equation of state in Mathwork’s Matlab. The final program required thousands of iterations of the EOS, so I was interested in optimizing the program to decrease run time. Before optimizing a piece of code, we must have a way of measuring and benchmarking speed for comparison. I used two tools for measuring my software’s speed.
Tic-Toc (tic, toc) – The clock tool is a simple method for measuring run time. Insert tic to start the clock, and toc each time you want time to be printed to the command window. Each insertion of tic will restart the clock from zero, while each toc prints the current clock time. It does not provide extensive information, but it is simple to apply and does not significantly slow down the software.
tic for i=1:100 x = y*z; end toc
Each toc will print the time as: Elapsed time is 333.005251 seconds.
Profile Tool (profile) – Matlab’s profile tool provides extensive run time information for software. It takes longer to implement so if you are analyzing a short piece of code, tic-toc will do the job better. Running the tool significantly increases the overall run time of the program, but the information it provides is worth it. The profile tool reports time spent on each function and in each line of code in the program. Bottlenecks are easily identified to help focus on the most time consuming lines of code. (see the bottom of the post for a sample report).
To run the profile tool, type profile on in the command window followed by profile clear to ensure previous records are cleared. Run the code to be analyzed, then type profreport(‘function_name’) to launch the report. You should use the top function in the program’s hierarchy because the report allows you to easily view each child function in further detail.
Total time is the time spent within each function and includes time spent in that function’s child functions. Self time is time spent for operations in the function. The function EOS_fun is the bottleneck of this program. The report will also include the run time of built in Matlab functions like the roots function above. Inlining Matlab functions can increase speed by removing function overhead. Clicking a particular function gives a detailed report for the function. Below are screen shots of a detailed report for EOS_fun.
The top of the report supplies the lines where the most time was spent in the code. It is apparent that optimizing line 97 could shave a significant amount of time for this program.
The profiler also reports the time spent in children functions, this one being the Matlab function roots, however it will report on any children functions including functions defined by the user. The code analyzer will point out areas of obvious inefficiencies like output (screen printing) surpression and changing variable sizes (variable definitions). Coverage results may point out lines of code that do not run. It is useful for identifying conditional statements that are never entered. Sometimes these conditions are required for the redundency of the program, but if they are not necessary they can save a program time.
Above is a portion of the function listing. This gives a report for each line of code. Specific problem areas can be identified on a per line basis. The reported numbers on the left are: total time | total calls | line. Profile conveniently shades the most time consuming lines from pink to red based on time consumed for each line in relation to the time spent in the function.
Profile reports conveniently save in HTML format. I have uploaded the report used for the examples in this post. Take a look at this complete profile report here.
Now you have the information you need to start speeding up that code!




[...] Testing Speed of Matlab Code [...]
Nice one. Learnt something new
Glad I could help you expand your knowledge, Chad.