matlab提速技巧(自matlab帮助文件)
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, 看是否有提速。
matlab提速技巧(自matlab帮助文件)的更多相关文章
- Matlab小技巧
记录一些用Matlab的技巧. //imshow全屏 subplot(1,3,3); imshow(topSketMat); hold on; set(gcf, 'units', 'normalize ...
- 使用 matlab 产生GK101任意波数据文件的方法
一.引言 MATLAB是由美国mathworks公司发布的主要面对科学计算.可视化以及交互式程序设计的高科技计算环境.它不但包含高效的数值计算.数据处理能力,而且简单易用,是工程师日常研发过程中不可缺 ...
- Matlab使用技巧
(1) Matlab强制退出正在运行的程序A: Ctrl + C(2)如何让Matlab跑完程序后自动关机?A: 在程序的末尾加上一条代码: system('shutdown -s') 当然 ...
- 小论文matlab作图技巧
小论文matlab作图技巧 编辑->复制选项 编辑->图形属性 图中右击->字型 编辑->复制图片,即可. 效果: 宽:5.9高: 7.91
- matlab的m程序转执行文件exe
转换主要有两步: 第一步 设置编译器 在命令窗口输入 mbuild -setup 根据提示操作即可,.如下图我的设置 第二步 转换执行文件 命令行输入 mcc -m main 即可(输入 mcc ...
- 设置MATLAB中Current Folder的默认文件夹(转载)
设置MATLAB中Current Folder的默认文件夹 在我们使用MATLAB的过程中,其Current Folder面板会给我们带来一定的便利性.但遗憾的是,MATLAB自身没有提供友好的设置界 ...
- Matlab 从入门到精通 Chapter11 文件读取I/O
11.1 工作空间数据读取 将工作空间的变量保存为文件,可以使用save命令. save('filename') 将文件保存在当前目录下,文件名为filename.mat save('filenam ...
- Matlab R2017b 关联 .m 和 .fig 文件
1. 前言 安装「Matlab R2017b」后,无法关联.m和.fig文件,每次需要在MATLAB里边打开,而不能之间点击.m文件打开,十分麻烦. 2. 解决方案 1.首先,在Matlab R201 ...
- (入门)matlab中创建和调用m文件
大学学过的一款软件,说实话没好好学,老师直接讲到高深的做仿真之类的 综合网上的教程讲述基础的matlab创建遇到的问题: 参考: 1. https://blog.csdn.net/weixin_423 ...
随机推荐
- ccrendertexture
int bgHeight=150; CCSprite *sp=CCSprite::create("HelloWorld.png"); sp->setAnchorPoint(c ...
- Customize the SharePoint 2013 search experience with a Content Enrichment web service
Did you ever wish you had more control over how your content is indexed and presented as search resu ...
- 《转载》CSS中的三种样式来源:创作人员、读者和用户代理
CSS中的样式一共有三种来源:创作人员.读者和用户代理,来源的不同会影响到样式的层叠方式,很多第一次学习CSS的朋友,对这三种来源可能会存在一些困惑,下面我写一下自己的理解,若有错误的地方还请指正. ...
- acrobat GetSize 返回 x,y 值单位
GetSize:LPDISPATCH GetSize();Description:Gets a page’s width and height in points.Parameters:Return ...
- while循环的跳出
今天在编码时突然产生一个疑问:程序中有一个while循环,循环体执行的是某个附带条件限制的操作.我现在想达到的目的是 => 条件成立,就执行操作,并跳出循环:条件不成立就跳出当次的while循环 ...
- 类库探源——System.Exception
一.MSDN描述 Exception 类: 表示在应用程序执行期间发生的错误 命名空间 : System 程序集: mscorlib.dll 继承关系: 常用属性(含字段)和方法: 1. 属性Me ...
- 理解java Web项目中的路径问题
本文以项目部署在tomcat服务器为例,其他相信也是一样的. 先说明请求页面的写法,在web中,页面路径主要写的有以下几种 1.请求重定向 2.浏览器的请求被服务器请求到新页面(我称为“转发”) 3. ...
- STP根交换机,指定端口,根端口,阻塞端口
STP判断各交换机端口(指定端口:DP;根端口:RP;阻塞端口:AP) 判断步骤: 1.选举根交换机: 判断对象:所有运行STP协议的交换机中选出一个; 判断依据:交换机中选择网桥ID值(交换机优先级 ...
- 一台机器,运行两个mysql版本 实例
一.问题 1.学习mysql时,我先安装了一个mysql5.5.27: 2.然而学习php时,当我安装wampserver2.2时,wampserver2.2又在电脑上安装了mysql5.5.20: ...
- 从外国html5网站上扒来一个鼠标经过的css3 效果,感觉很不错
鼠标经过的时候,感觉有点像一张纸卷上去的感觉. 下面是代码 <div class="main-container types"> <div class=" ...