没有合适的资源?快使用搜索试试~ 我知道了~
很好的书籍啊。这是有关如何提高matlab运行速度的书籍啊
资源详情
资源评论
资源推荐
Pascal Getreuer, January 2008
Contents
1 The Profiler 2
2 Array Preallocation 4
3 JIT Acceleration 6
4 Vectorization 8
4.1 Vectorized Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.2 Vectorized Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5 Inlining Simple Functions 13
6 Referencing Operations 15
7 Numerical Integration 18
7.1 One-Dimensional Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
7.2 Multidimensional Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
8 Signal Processing 22
9 Miscellaneous Tricks 25
9.1 Clip a value without using if statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
9.2 Convert any array into a column vector . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
9.3 Find the min/max of a matrix or N-d array . . . . . . . . . . . . . . . . . . . . . . . . . 25
9.4 Flood filling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
9.5 Vectorized use of set on GUI objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
10 Further Reading 27
Introduction
Matlab code is interpreted during runtime. Languages like C and Fortran are faster because they are
first compiled into the computer’s native language. Interpreting in realtime enables better platform
independence, greater language flexibility, and interactive debugging, but at the cost of slower speed,
increased overhead, and limited low-level control.
This article discusses strategies for improving the speed of Matlab code. Keep in mind that speed
depends heavily on the computation platform and that the fastest method on one system is not neces-
sarily the fastest on another. Second, Matlab has gone through many version and continues to evolve:
a slow method may become fast in a newer version. This article provides methods that are generally
fast, but makes no claim on what is fastest.
Caution!
Learn the language first. Optimization requires comfort with the syntax and features of the
language. This article is not a tutorial on MATLAB.
Use comments. Optimized code tends to be terse and cryptic. Help others and yourself by
remembering to comment.
Don’t optimize code before its time. Is optimization worth the effort? If the code will soon
be revised or extended, it will be rewritten anyway.
Only optimize where necessary. Focus your efforts on bottlenecks.
1
1
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
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 some overhead when running code for the first time; to remove this overhead from the timing,
run example1 once before profiling. 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
Clo ck precision: 0.016 s
Function List
2
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 analysis from the M-Lint
code analyzer. Potential problems and suggested improvements are listed here.
M-Lint results
Line number Message
4 ‘result’ might be growing inside a lo op. Consider preallocating for speed.
7 ‘result’ might be growing inside a lo op. Consider preallocating for speed.
For this example, M-Lint recommends using preallocation to improve lines 4 and 7 (preallocation is
discussed in the next section). You can also run M-Lint on its own using the mlint command.
The profiler has limited time resolution, so to profile a piece of code that runs too quickly, run it multiple
times with a loop:
>> profile on, profile clear
>> for k = 1:100, example; end
>> profile report
Adjust the number of loop iterations so that the time it takes to run is noticeable. More iterations
yields better time resolution.
3
2
Array Preallocation
Matlab’s matrix variables have the ability to dynamically augment rows and columns. For example,
>> a = 2
a =
2
>> a(2,6) = 1
a =
2 0 0 0 0 0
0 0 0 0 0 1
Matlab automatically resizes the matrix. Internally, the matrix data memory must be reallocated
with larger size. If a matrix is resized repeatedly—like within a loop—this overhead can be significant.
To avoid frequent reallocations, preallocate the matrix with the zeros command.
Consider the code:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
a(1) = 1;
b(1) = 0;
for k = 2:8000
a(k) = 0.99803
*
a(k − 1) − 0.06279
*
b(k − 1);
b(k) = 0.06279
*
a(k − 1) + 0.99803
*
b(k − 1);
end
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The profiler timed this code to take 0.47 seconds. After the for loop, both arrays are row vectors of
length 8000, thus to preallocate, create empty a and b row vectors each with 8000 elements.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
a = zeros(1,8000); % Preallocation
b = zeros(1,8000);
a(1) = 1;
b(1) = 0;
for k = 2:8000
a(k) = 0.99803
*
a(k − 1) − 0.06279
*
b(k − 1);
b(k) = 0.06279
*
a(k − 1) + 0.99803
*
b(k − 1);
end
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
With this modification, the code takes only 0.14 seconds (over three times faster). Preallocation is often
easy to do, in this case it was only necessary to determine the right preallocation size and add two lines.
4
本页已使用福昕阅读器进行编辑。
福昕软件(C)2005-2009,版权所有,
仅供试用。
剩余28页未读,继续阅读
lh819548714
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0