Writing Fast Matlab Code #2: Profile

October 7, 2009 by Pascal Getreuer 
Filed under: Code Optimization, Tutorials 
Leave a Comment
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
VN:F [1.8.8_1072]
Rating: 0.0/10 (0 votes cast)

Writing Fast Matlab code

The Profiler

Matlab 5.0 (R10) and newer versions include a tool called the profiler that helps identify  bottlenecks in a program.  Use it with the profile command:

profile on     % Turn  the profiler on
 
profile off   %Turn  it back off
 
profile clear    %Clear profile statistics
 
profile report  %View the results  from the profiler

For example,  consider profiling the following function:

function result  =  example1(Count)
 
for k  =  1:Count
 
result(k) =  sin(k/50);
 
if result(k) < −0.9 result(k) =  gammaln(k);
 
end

To analyze  the  efficiency this  function,  first enable  and  clear the  profiler,  run  the  function,  and  then view the profile report:

profile on
 
profile clear
 
example1(5000);
 
profile report

There is a slight parsing overhead when running  code for the first time; run the test code twice and time the  second run.   The  profiler report command  shows a report.   Depending  on the  system,  profiler results  may differ from this example.

MATLAB Profile Report: Summary

Report generated 30-Jul-2004 16:57:01

Total recorded time: 3.09 s

Number of M-functions: 4

Clock precision: 0.016 s

Function List

Name Time                  Time     Time/call Self time         Location

example1

3.09

100.0%

1

3.094000

2.36

76.3%

~/example1.m

gammaln

0.73

23.7%

3562

0.000206

0.73

23.7%

../toolbox/matlab/specfun/gammaln.m

profile

0.00

0.0%

1

0.000000

0.00

0.0%

../toolbox/matlab/general/profile.m

profreport

0.00

0.0%

1

0.000000

0.00

0.0%

../toolbox/matlab/general/profreport.m

Clicking the “example1” link gives more details:

Lines where the most time was spent

Line Number

Code

Calls

Total Time

% Time

4

result(k) = sin(k/50);

5000

2.11 s

68%

7

result(k) = gammaln(k);

721

0.84 s

27%

6

if result(k) < -0.9

5000

0.14 s

5%

Totals

3.09 s

100%

The most time-consuming lines are displayed,  along with time,  time percentage, and line number.  The most costly lines are the computations on lines 4 and 7.

Another  helpful section of the profile report  is “M-Lint Results,”  which gives feedback from the M-Lint code analyzer.  Possible errors and suggestions  are listed here.

M-Lint results

Line number Message

4 result might be growing inside a loop. Consider preallocating for speed.

7 result might be growing inside a loop. Consider preallocating for speed.

(Preallocation is discussed in the next section.)

The profiler has limited  time resolution,  so to profile a piece of code that runs too quickly, run the test code multiple  times with a loop.  Adjust  the number  of loop iterations so that the time it takes  to run is noticeable.  More iterations yields better time resolution.

The  profiler  is an  essential  tool  for identifying  bottlenecks and  per-statement analysis,  however,  for more accurate  timing  of a piece of code, use the tic/toc stopwatch timer.

tic;
 
example1(5000);
 
toc;
Elapsed time is  3.082055 seconds

For serious benchmarking, also close your web browser, anti-virus, and other background processes that may be taking  CPU  cycles.

Download Full Pdf Version

Function List

Name Time                  Time     Time/call Self time         Location

example1

3.09

100.0%

1

3.094000

2.36

76.3%

~/example1.m

gammaln

0.73

23.7%

3562

0.000206

0.73

23.7%

../toolbox/matlab/specfun/gammaln.m

profile

0.00

0.0%

1

0.000000

0.00

0.0%

../toolbox/matlab/general/profile.m

profreport

0.00

0.0%

1

0.000000

0.00

0.0%

../toolbox/matlab/general/profreport.m

Clicking the “example1” link gives more details:

Lines where the most time was spent

Line Number

Code

Calls

Total Time

% Time

4

result(k) = sin(k/50);

5000

2.11 s

68%

7

result(k) = gammaln(k);

721

0.84 s

27%

6

if result(k) < -0.9

5000

0.14 s

5%

Totals

3.09 s

100%

VN:F [1.8.8_1072]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)

Popularity: 1% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Live
  • PDF
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Add to favorites
  • email
  • MySpace
  • RSS

Related Posts

Comments

Comments are closed.