博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
matlab提速技巧(自matlab帮助文件)
阅读量:5949 次
发布时间:2019-06-19

本文共 2884 字,大约阅读时间需要 9 分钟。

1.首先要学会用profiler.

1.1. 打开profiler.
To open the Profiler, select View -> Profiler from the MATLAB desktop, or type profile viewer in the Command Window. The MATLAB Profiler opens.
在我的机器上是: 在matlab desktop下,Desktop->Profiler.
在M文件编辑器下,Tools->Open Profiler.
1.2. 运行profiler
可以把要运行的code拷入Run this code后面的输入框里。
You can run this example
[t,y] = ode23('lotka',[0 2],[20;20])
也可以输入要运行的M文件名。
1.3.Click Start Profiling (or press Enter after entering the statement).
1.4. 查看Profile Detail Report
会告知你哪些代码消耗了多少时间,可以找到哪些函数或那些代码行消耗了主要的时间,或者是经常被调用。

也可以用stopwatch Timer函数,计算程序消耗时间

Use tic and toc as shown here.
tic
   - run the program section to be timed -
toc

2. 加速1:向量化
MATLAB is a matrix language, which means it is designed for vector and matrix operations. You can often speed up your M-file code by using vectorizing algorithms that take advantage of this design. Vectorization means converting for and while loops to equivalent vector or matrix operations.

i = 0;

for t = 0:.01:1000
    i = i+1;
    y(i) = sin(t);
end

运行时间为30.776秒。

改为向量化代码:
t = 0:.01:1000;
y = sin(t);
运行时间为0秒。

Functions Used in Vectorizing

Some of the most commonly used functions for vectorizing are:
 all
 diff
 ipermute
 permute
 reshape
 squeeze
 any
 find
 logical
 prod
 shiftdim
 sub2ind
 cumsum
 ind2sub
 ndgrid
 repmat
 sort
 sum
 
 3. 加速2:Preallocating Arrays(预分配空间)
You can often improve code execution time by preallocating the arrays that store output results. Preallocation makes it unnecessary for MATLAB to resize an array each time you enlarge it. Use the appropriate preallocation function for the kind of array you are working with.
Preallocation also helps reduce memory fragmentation if you work with large matrices.

4.加速其他方法:

Coding Loops in a MEX-File for Speed

If there are instances where you must use a for loop, consider coding the loop in a MEX-file. In this way, the loop executes much more quickly since the instructions in the loop do not have to be interpreted each time they execute.

Functions Are Faster Than Scripts

Your code will execute more quickly if it is implemented in a function rather than a script. Every time a script is used in MATLAB, it is loaded into memory and uated one line at a time. Functions, on the other hand, are compiled into pseudo-code and loaded into memory once. Therefore, additional calls to the function are faster.

Load and Save Are Faster Than File I/O Functions

If you have a choice of whether to use load and save instead of the MATLAB file I/O routines, choose the former. The load and save functions are optimized to run faster and reduce memory fragmentation.

Avoid Large Background Processes

Avoid running large processes in the background at the same time you are executing your program in MATLAB. This frees more CPU time for your MATLAB session.

 

 5. 多线程

在matlab desktop里,File->Preferences->General->Multithreading, 看是否选择了Enable Multithreaded Computation。

如果没选,check it, 看是否有提速。

转载地址:http://yoixx.baihongyu.com/

你可能感兴趣的文章
做错的题目——给Array附加属性
查看>>
Url.Action取消字符转义
查看>>
K8S调度之标签选择器
查看>>
JQuery选择器大全
查看>>
Gamma阶段第三次scrum meeting
查看>>
python3之装饰器修复技术@wraps
查看>>
C# unity零碎知识点笔记(容易混淆的一些点)3
查看>>
[考试]20150606
查看>>
Javascript_备忘录5
查看>>
Can’t create handler inside thread that has not called Looper.prepare()
查看>>
敏捷开发方法综述
查看>>
Hadoop数据操作系统YARN全解析
查看>>
Django 运行报错 ImportError: No module named 'PIL'
查看>>
修改数据库的兼容级别
查看>>
Windows下同时安装两个版本Jdk
查看>>
文件上传到阿里云
查看>>
网络知识
查看>>
uoj#228. 基础数据结构练习题(线段树)
查看>>
iptables 端口转发--内网实现上网
查看>>
计蒜客NOIP模拟D1T2
查看>>