MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
Consider keeping a copy of your first detail report as a basis for comparison. After
you change your code, you can run the Profiler again and compare the reports.
4 Determine whether there are changes you can make to those lines of code to improve
performance.

For example, if you have a load statement within a loop, you might be able to move
the load statement outside the loop so that it is called only once.
5 Implement the potential performance improvements in your code. Save the files, and
run clear all. Run the Profiler again and compare the results to the original
report.

If you profile the identical code twice, you can get slightly different results each time
due to inherent time fluctuations that are not dependent on your code.
6 To continue improving the performance of your code, repeat these steps.

When your code spends most of its time on calls to a few built-in functions, you have
probably optimized the code as much as possible.

Using the Profiler


To profile a MATLAB code file or a line of code:

1 Open the Profiler using one of the following methods:


  • In the Command Window, type profile viewer.


  • On the Home tab, in the Code section, click Run and Time.




  • In the Editor, on the Editor tab, in the Run section, click Run and Time. If
    you use this method, the Profiler automatically profiles the code in the current
    Editor tab. If that is the code you want to profile, skip to step 4.
    2 In the Run this code field, type the statement you want to run.




For example, you can run the Lotka-Volterra example, which is provided with
MATLAB:

[t,y] = ode23('lotka',[0 2],[20;20])

If, in the current MATLAB session, you previously profiled the statement, select it
from the Run this code list. MATLAB automatically starts profiling the code, and you
can skip to step 4.

28 Performance

Free download pdf