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 ...
随机推荐
- 开源免费天气预报接口API以及全国全部地区代码!!(国家气象局提供)
国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...
- cocos2d-x 2.0.3 设置高清模式注意事项(已移除-hd方式)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=304 在cocos2d-x 2. ...
- ASP.NET中如何实现负载均衡
ASP.NET站点中做负载均衡: 基于HTTP协议我们可能发现我们要解决两点问题: 第一,做到负载均衡,我们需要一个负载均衡器. 可以通过DNS轮询来做,在DNS服务器上配置为每次对我们做负载均衡的同 ...
- Java基础知识强化之集合框架笔记61:Map集合之统计字符串中每个字符出现的次数的案例
1. 首先我们看看统计字符串中每个字符出现的次数的案例图解: 2. 代码实现: (1)需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5) ...
- Java的浮点数
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/5160771. ...
- CSS相对定位、绝对定位
CSS定位属性:position. 定位的基本思想:定义元素框相对于其正常位置应该出现的位置,或者相对于父元素.另一个元素或浏览器窗口本身的位置. position属性值:static.relativ ...
- javascript的面向对象编程
面象对象编程技术的核心理念:封装.继承.多态:在一些主流的高级编程语言中,比如:C#,VB.NET,JAVA,PHP等都是很容易实现的,而如果要在javascript中实现面象对象编程,可就不那么直接 ...
- Spring各种传播特性源码实现的概览
这几天都在分析Spring的源码实现,看到事务管理的部分 我们知道事务的传播特性有下面几种,我标黄的就是最常用的3中传播特性, Sping在发生事务嵌套的时候,会依据内层事务的传播特性,来决定内层是事 ...
- Nico Game Studio 2.设置页面读写 纹理载入与选择
进度十分之慢... 配置读写一样采用之前写的自动绑定的方法: 分享一下代码: SetControl是把数据写到control上的. SetObject是把数据写到对象上 GetData是从控件读取数据 ...
- ASP.NET MVC学习系列 WebAPI初探
转自http://www.cnblogs.com/babycool/p/3922738.html 一.无参数Get请求 一般的get请求我们可以使用jquery提供的$.get() 或者$.ajax( ...