热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Matlab调用帮助文件,matlab提速技巧(自matlab帮助文件)

1.首先要学会用profiler分析代码效率.1.1.打开profiler.在我的机器上是:在matlabdesktop下,Desktop-Profi

1.首先要学会用profiler分析代码效率.

1.1. 打开profiler.

在我的机器上是:在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 programs areinterpretted.This would seem to make it inapproapriate for large scale scientific computing. The power of MATLAB is realized with its extensive set of libraries which are compiled or are carefully coded in MATLAB to utilize "vectorization". The concept of vectorization is central to understanding how to write efficient MATLAB code.

Vectorized code takes advantage, wherever possible, of operations involving data stored as vectors. This even applies to matrices since a MATLAB matrix is stored (by columns) in contiguous locations in the computer's RAM. The speed of a numerical algorithm in MATLAB is very sensitive to whether or not vectorized operations are used.

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秒。

这段代码是很自然的从C语言的形式转化而来的,但其效率很低!Matlab是实时为变量分配内存的,在第一遍循环时(即i=1时),Matlab为y向量(长度为1)分配内存。以后每执行一次循环,Matlab都会在y的末尾附加新的元素。这不仅导致分配内存的调用的增加,也使得y的各个元素在内存中的分布不是连续的(就像数据结构中数组和链表的区别)。相比之下,下面的代码效率提高不少:

改为向量化代码:

t = 0:.01:1000;

y = sin(t);

运行时间为0秒。

第一个语句分配了一个连续的内存空间来存储具有多个元素的向量t。类似的,第二个语句在分配内存时,也是分配了一个连续的内存空间来存储具有多个元素的向量y。撇去计算sin的消耗不算,就内存分配命令的执行次数和对向量元素访问的方便程度来说,高下立见。

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 helpsreduce memory fragmentationif you work with large matrices.

虽然Matlab会自动调整变量的大小,我们最好还是预先为变量分配内存空间。因为这样可以使调用内存分配命令的次数降为1,也可以使变量在内存中连续存储(当变量为矩阵时是按列在内存中连续存储)。而所谓“预先为变量分配内存空间” ,是指在知道变量的大小的情况下,在变量中的任何一个元素都未被引用之前,创建一个大小和其一致的变量。

例如,下面的代码自上而下,运行效率是不断提高的:

1 dx = pi/30;

2 nx =1+2*pi/dx;

3 nx2 = nx/2;

4

5 fori =1:nx2

6 x(i) = (i-1)*dx;

7 y(i) =sin(3*x(i));

8 end

9

10 fori = nx2+1:nx

11 x(i) = (i-1)*dx;

12 y(i) =sin(5*x(i));

13 end

1 dx = pi/30;

2 nx =1+2*pi/dx;

3 nx2 = nx/2;

4

5x = zeros(1,nx);      %为向量x预分配内存

6y = zeros(1,nx);      %为向量y预分配内存

7

8 fori =1:nx2

9 x(i) = (i-1)*dx;

10 y(i) =sin(3*x(i));

11 end

12

13 fori = nx2+1:nx

14 x(i) = (i-1)*dx;

15 y(i) =sin(5*x(i));

16 end

1x =0:pi/30:2*pi;     %计算向量x的值

2 nx = length(x);

3 nx2 = nx/2;

4

5y = x;                %为向量y预分配内存

6

7 fori =1:nx2

8 y(i) =sin(3*x(i));

9 end

10

11 fori = nx2+1:nx

12 y(i) =sin(5*x(i));

13 end

1x =0:pi/30:2*pi;                  %计算向量x的值

2 nx = length(x);

3 nx2 = nx/2;

4

5y = x;                             %为向量y预分配内存

6

7y(1:nx2) =sin(3*x(1:nx2));        %计算y的第1部分的值

8y(nx2+1:nx) =sin(5*x(nx2+1:nx));  %计算y的第2部分的值

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, 看是否有提速。



推荐阅读
  • NN,NearestNeighbor,最近邻KNN,K-NearestNeighbor,K最近邻KNN分类的思路:分类的过程其实是直接将测试集的每一个图片和训练集中的所有图片进行比 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • 摘要:本文从介绍基础概念入手,探讨了在CC++中对日期和时间操作所用到的数据结构和函数,并对计时、时间的获取、时间的计算和显示格式等方面进行了阐述。本文还通过大量的实例向你展示了t ... [详细]
  • 1print过程procprint<data数据集名><选项>;*label指定打印输出标签noobs制定不显示观测序号*by变量名1< ... [详细]
  • 2018年人工智能大数据的爆发,学Java还是Python?
    本文介绍了2018年人工智能大数据的爆发以及学习Java和Python的相关知识。在人工智能和大数据时代,Java和Python这两门编程语言都很优秀且火爆。选择学习哪门语言要根据个人兴趣爱好来决定。Python是一门拥有简洁语法的高级编程语言,容易上手。其特色之一是强制使用空白符作为语句缩进,使得新手可以快速上手。目前,Python在人工智能领域有着广泛的应用。如果对Java、Python或大数据感兴趣,欢迎加入qq群458345782。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 本文讨论了微软的STL容器类是否线程安全。根据MSDN的回答,STL容器类包括vector、deque、list、queue、stack、priority_queue、valarray、map、hash_map、multimap、hash_multimap、set、hash_set、multiset、hash_multiset、basic_string和bitset。对于单个对象来说,多个线程同时读取是安全的。但如果一个线程正在写入一个对象,那么所有的读写操作都需要进行同步。 ... [详细]
  • 本文介绍了Python语言程序设计中文件和数据格式化的操作,包括使用np.savetext保存文本文件,对文本文件和二进制文件进行统一的操作步骤,以及使用Numpy模块进行数据可视化编程的指南。同时还提供了一些关于Python的测试题。 ... [详细]
  • 深入理解Java虚拟机的并发编程与性能优化
    本文主要介绍了Java内存模型与线程的相关概念,探讨了并发编程在服务端应用中的重要性。同时,介绍了Java语言和虚拟机提供的工具,帮助开发人员处理并发方面的问题,提高程序的并发能力和性能优化。文章指出,充分利用计算机处理器的能力和协调线程之间的并发操作是提高服务端程序性能的关键。 ... [详细]
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
  • Vue基础一、什么是Vue1.1概念Vue(读音vjuː,类似于view)是一套用于构建用户界面的渐进式JavaScript框架,与其它大型框架不 ... [详细]
author-avatar
mobiledu2502873827
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有