Accelerating Matlab

Matlab is a very useful programming environment, but it also has many inefficiencies. You might think that these are unavoidable, but in fact it is possible to fix most of them, without significantly changing your programs. This page describes some easy ways to modify the Matlab environment to make programs run faster.

  1. Install Marcel Leutenegger's package of elementary functions. They speed up functions like exp and log by a factor of 3 or more, with no loss of accuracy. They directly replace the functions in Matlab, so no program modification is required.
  2. Run mex -setup and select a good compiler. The default compiler (lcc) does not produce very good code.

    To use Microsoft Visual Studio .NET 2003 version 7.1, you will first need to install a patch. Unfortunately, while Visual Studio 7.1 generally produces good code, it has a performance bug in the intrinsic exp function. To get around this, edit the mex options file (C:\MATLAB6p5p1\bin\win32\mexopts\msvc71opts.bat) to read:

    set OPTIMFLAGS=/MD -O2 -Oy- /Oi- -DNDEBUG

    After changing the compiler, you should re-compile your mex files, and re-install any packages including mex (such as lightspeed).

     

  3. Install lightspeed. It provides optimized implementations of common operations, including a C replacement for repmat.m.
  4. Profile your code to find bottlenecks:
    profile on
    myfun;
    profile report
    
  5. Avoid loops by writing 'vectorized' code. See the MathWorks' Vectorization Guide, Marios Athineos's tips and tricks, and the routines in lightspeed (such assqdist).
Efficient ways to do common tasks
Manipulate sets of integers

The fastest way to do this is with sparse logical vectors. If you want to use sorted arrays of integers instead, beware that the Matlab functions setdiff, union, etc. are not optimized for this case and will be a bottleneck. Optimized functions for the sorted case are included in lightspeed.

Represent a graph

Use a sparse logical adjacency matrix, and use matrix operations whenever possible. For example, if G is symmetric (i.e. an undirected graph) then G*G gives the number of neighbors in common to nodes i and j, for all (i,j). See Kevin Murphy's graph toolbox.

Sample random numbers from various distributions

Use the functions provided in lightspeed.

Read XML

Peter Rysadter's XML parser was the fastest, but is no longer available. Check out the links at Undocumented XML functionality.

 

文章转载自 Tom MinkaAccelerating Matlab

转载于:https://www.cnblogs.com/VVingerfly/p/4722449.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Accelerating MATLAB Performance - 1001 Tips to Speed Up MATLAB Programs MATLAB 程序加速技巧 http://undocumentedmatlab.com/books/matlab-performance Accelerating MATLAB Performance: 1001 Tips to Speed Up MATLAB Programs by Yair M. Altman English | 2014 | ISBN: 1482211300 | 785 pages | PDF | 145 MB Features Demonstrates how to improve MATLAB® speed in many different ways Shows how to profile MATLAB code to identify performance hotspots Discusses various tradeoffs in MATLAB performance tuning Addresses vectorization, parallelization, distributed computing, caching, optimization, memory management, GUI, graphics, and I/O Explains using GPU, MEX, MATLAB toolboxes and external libraries Contains programming tips useful to novices and professionals alike Includes thousands of tips, code examples, and online references Supported by an active website Summary The MATLAB® programming environment is often perceived as a platform suitable for prototyping and modeling but not for "serious" applications. One of the main complaints is that MATLAB is just too slow. Accelerating MATLAB Performance aims to correct this perception by describing multiple ways to greatly improve MATLAB program speed. Packed with thousands of helpful tips, it leaves no stone unturned, discussing every aspect of MATLAB. Ideal for novices and professionals alike, the book describes MATLAB performance in a scale and depth never before published. It takes a comprehensive approach to MATLAB performance, illustrating numerous ways to attain the desired speedup. The book covers MATLAB, CPU, and memory profiling and discusses various tradeoffs in performance tuning. It describes both the application of standard industry techniques in MATLAB, as well as methods that are specific to MATLAB such as using different data types or built-in functions. The book covers MATLAB vectorization, parallelization (implicit and explicit), optimization, memory management, chunking, and caching. It explains MATLAB’s memory model and details how it can be leveraged. It describes the use of GPU, MEX, FPGA, and other forms of compiled code, as well as techniques for speeding up deployed applications. It details specific tips for MATLAB GUI, graphics, and I/O. It also reviews a wide variety of utilities, libraries, and toolboxes that can help to improve performance. Sufficient information is provided to allow readers to immediately apply the suggestions to their own MATLAB programs. Extensive references are also included to allow those who wish to expand the treatment of a particular topic to do so easily. Supported by an active website, and numerous code examples, the book will help readers rapidly attain significant reductions in development costs and program run times.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值