SlideShare a Scribd company logo
Writing Fast MATLAB Code
Jia-Bin Huang
University of Illinois, Urbana-Champaign
www.jiabinhuang.com
jbhuang1@Illinois.edu
Resources
• Techniques for Improving Performance by Mathwork
• Writing Fast Matlab Code by Pascal Getreuer
• Guidelines for writing clean and fast code in MATLAB by Nico Schlömer
• http://www.slideshare.net/UNISTSupercomputingCenter/speeding-
upmatlabapplications
• http://www.matlabtips.com/
Using the Profiler
• Helps uncover performance problems
• Timing functions:
• tic, toc
• The following timings were measured on
- CPU i5 1.7 GHz
- 4 GB RAM
• http://www.mathworks.com/help/matlab/ref/profile.html
Pre-allocation Memory
3.3071 s
>> n = 1000;
2.1804 s2.5148 s
Reducing Memory Operations
>> x = 4;
>> x(2) = 7;
>> x(3) = 12;
>> x = zeros(3,1);
>> x = 4;
>> x(2) = 7;
>> x(3) = 12;
Vectorization
• http://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
2.1804 s 0.0157 s
139x faster!
Using Vectorization
• Appearance
• more like the mathematical expressions, easier to understand.
• Less Error Prone
• Vectorized code is often shorter.
• Fewer opportunities to introduce programming errors.
• Performance:
• Often runs much faster than the corresponding code containing loops.
See http://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
Binary Singleton Expansion Function
• Make each column in A zero mean
>> n1 = 5000;
>> n2 = 10000;
>> A = randn(n1, n2);
• See http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-
performance/
0.2994 s 0.2251 s
Why bsxfun is faster than repmat?
- bsxfun handles replication of the array
implicitly, thus avoid memory allocation
- Bsxfun supports multi-thread
Loop, Vector and Boolean Indexing
• Make odd entries in vector v zero
• n = 1e6;
• See http://www.mathworks.com/help/matlab/learn_matlab/array-indexing.html
• See Fast manipulation of multi-dimensional arrays in Matlab by Kevin Murphy
0.3772 s 0.0081 s 0.0130 s
Solving Linear Equation System
0.1620 s 0.0467 s
Dense and Sparse Matrices
• Dense: 16.1332 s
• Sparse: 0.0040 s
More than 4000x
faster!
Useful functions:
sparse(), spdiags(),
speye(), kron().
0.6424 s 0.1157 s
Repeated solution of an equation system with
the same matrix
3.0897 s 0.0739 s
Iterative Methods for Larger Problems
• Iterative solvers in MATLAB:
• bicg, bicgstab, cgs, gmres, lsqr, minres, pcg, symmlq, qmr
• [x,flag,relres,iter,resvec] = method(A,b,tol,maxit,M1,M2,x0)
• source: Writing Fast Matlab Code by Pascal Getreuer
Solving Ax = b when A is a Special Matrix
• Circulant matrices
• Matrices corresponding to cyclic convolution
Ax = conv(h, x) are diagonalized in the Fourier domain
>> x = ifft( fft(b) ./ fft(h) );
• Triangular and banded
• Efficiently solved by sparse LU factorization
>> [L,U] = lu(sparse(A));
>> x = U(Lb);
• Poisson problems
• See http://www.cs.berkeley.edu/~demmel/cs267/lecture25/lecture25.html
In-place Computation
>> x=randn(1000,1000,50);
0.1938 s 0.0560 s
Inlining Simple Functions
1.1942 s 0.3065 s
functions are worth inlining:
- conv, cross, fft2, fliplr, flipud, ifft, ifft2, ifftn, ind2sub, ismember, linspace, logspace, mean,
median, meshgrid, poly, polyval, repmat, roots, rot90, setdiff, setxor, sortrows, std, sub2ind,
union, unique, var
y = medfilt1(x,5); 0.2082 s
Using the Right Type of Data
“Do not use a cannon to kill a mosquito.”
double image: 0.5295 s
uint8 image: 0.1676 s
Confucius
Matlab Organize its Arrays as Column-Major
• Assign A to zero row-by-row or column-by-column
>> n = 1e4;
>> A = randn(n, n);
0.1041 s2.1740 s
Column-Major Memory Storage
>> x = magic(3)
x =
8 1 6
3 5 7
4 9 2
% Access one column
>> y = x(:, 1);
% Access one row
>> y = x(1, :);
Copy-on-Write (COW)
>> n = 500;
>> A = randn(n,n,n);
0.4794 s 0.0940 s
Clip values
>> n = 2000;
>> lowerBound = 0;
>> upperBound = 1;
>> A = randn(n,n);
0.0121 s0.1285 s
Moving Average Filter
• Compute an N-sample moving average of x
>> n = 1e7;
>> N = 1000;
>> x = randn(n,1);
3.2285 s 0.3847 s
Find the min/max of a matrix or N-d array
>> n = 500;
>> A = randn(n,n,n);
0.5465 s
0.1938 s
Acceleration using MEX (Matlab Executable)
• Call your C, C++, or Fortran codes from the MATLAB
• Speed up specific subroutines
• See http://www.mathworks.com/help/matlab/matlab_external/introducing-mex-
files.html
MATLAB Coder
• MATLAB Coder™ generates standalone C and C++ code from
MATLAB® code
• See video examples in http://www.mathworks.com/products/matlab-
coder/videos.html
• See http://www.mathworks.com/products/matlab-coder/
DoubleClass
• http://documents.epfl.ch/users/l/le/leuteneg/www/MATLABToolbox/
DoubleClass.html
parfor for parallel processing
• Requirements
• Task independent
• Order independent
See http://www.mathworks.com/products/parallel-computing/
Parallel Processing in Matlab
• MatlabMPI
• multicore
• pMatlab: Parallel Matlab Toolbox
• Parallel Computing Toolbox (Mathworks)
• Distributed Computing Server (Mathworks)
• MATLAB plug-in for CUDA (CUDA is a library that used an nVidia
board)
• Source: http://www-h.eng.cam.ac.uk/help/tpl/programs/Matlab/faster_scripts.html
Resources for your final project
• Awesome computer vision by Jia-Bin Huang
• A curated list of computer vision resources
• VLFeat
• features extraction and matching, segmentation, clustering
• Piotr's Computer Vision Matlab Toolbox
• Filters, channels, detectors, image/video manipulation
• OpenCV (MexOpenCV by Kota Yamaguchi)
• General purpose computer vision library

More Related Content

What's hot (20)

Python Programming
Python ProgrammingPython Programming
Python Programming
Sreedhar Chowdam
 
タクラムと考える未来の文化創造に必要なアーキテクチャ思考 先生:Kaz 米田
タクラムと考える未来の文化創造に必要なアーキテクチャ思考 先生:Kaz 米田タクラムと考える未来の文化創造に必要なアーキテクチャ思考 先生:Kaz 米田
タクラムと考える未来の文化創造に必要なアーキテクチャ思考 先生:Kaz 米田
schoowebcampus
 
Python-List comprehension
Python-List comprehensionPython-List comprehension
Python-List comprehension
Colin Su
 
200604material ozaki
200604material ozaki200604material ozaki
200604material ozaki
RCCSRENKEI
 
行列およびテンソルデータに対する機械学習(数理助教の会 2011/11/28)
行列およびテンソルデータに対する機械学習(数理助教の会 2011/11/28)行列およびテンソルデータに対する機械学習(数理助教の会 2011/11/28)
行列およびテンソルデータに対する機械学習(数理助教の会 2011/11/28)
ryotat
 
多チャンネルバイラテラルフィルタの高速化
多チャンネルバイラテラルフィルタの高速化多チャンネルバイラテラルフィルタの高速化
多チャンネルバイラテラルフィルタの高速化
Norishige Fukushima
 
Modul mesin bubut 7 (5)
Modul mesin bubut 7 (5)Modul mesin bubut 7 (5)
Modul mesin bubut 7 (5)
Eko Supriyadi
 
球面フィッティングの導出と実装
球面フィッティングの導出と実装球面フィッティングの導出と実装
球面フィッティングの導出と実装
j_rocket_boy
 
Introduction to-python
Introduction to-pythonIntroduction to-python
Introduction to-python
Aakashdata
 
Q1 Memory Fabric Forum: Memory expansion with CXL-Ready Systems and Devices
Q1 Memory Fabric Forum: Memory expansion with CXL-Ready Systems and DevicesQ1 Memory Fabric Forum: Memory expansion with CXL-Ready Systems and Devices
Q1 Memory Fabric Forum: Memory expansion with CXL-Ready Systems and Devices
Memory Fabric Forum
 
Transient three dimensional cfd modelling of ceilng fan
Transient three dimensional cfd modelling of ceilng fanTransient three dimensional cfd modelling of ceilng fan
Transient three dimensional cfd modelling of ceilng fan
Lahiru Dilshan
 
♥3♥مبادئ الفلسفة 101 بول كلينمان.pdf
♥3♥مبادئ الفلسفة 101 بول كلينمان.pdf♥3♥مبادئ الفلسفة 101 بول كلينمان.pdf
♥3♥مبادئ الفلسفة 101 بول كلينمان.pdf
khatab719
 
API Developer Training: Insights for Integrations
API Developer Training: Insights for IntegrationsAPI Developer Training: Insights for Integrations
API Developer Training: Insights for Integrations
JeremyOtt5
 
Model Integration
Model IntegrationModel Integration
Model Integration
Seyed Faridoddin Kiaei
 
MemVerge: The Software Stack for CXL Environments
MemVerge: The Software Stack for CXL EnvironmentsMemVerge: The Software Stack for CXL Environments
MemVerge: The Software Stack for CXL Environments
Memory Fabric Forum
 
Hardware-assisted Isolated Execution Environment to run trusted OS and applic...
Hardware-assisted Isolated Execution Environment to run trusted OS and applic...Hardware-assisted Isolated Execution Environment to run trusted OS and applic...
Hardware-assisted Isolated Execution Environment to run trusted OS and applic...
Kuniyasu Suzaki
 
Introduction to python 3
Introduction to python 3Introduction to python 3
Introduction to python 3
Youhei Sakurai
 
IntelON 2021 Processor Benchmarking
IntelON 2021 Processor BenchmarkingIntelON 2021 Processor Benchmarking
IntelON 2021 Processor Benchmarking
Brendan Gregg
 
Franco y la guerra civil española1
Franco y la guerra civil española1Franco y la guerra civil española1
Franco y la guerra civil española1
Neil Jones
 
JSR381 Visual Recognition for Java.pdf
JSR381 Visual Recognition for Java.pdfJSR381 Visual Recognition for Java.pdf
JSR381 Visual Recognition for Java.pdf
Zoran Sevarac, PhD
 
タクラムと考える未来の文化創造に必要なアーキテクチャ思考 先生:Kaz 米田
タクラムと考える未来の文化創造に必要なアーキテクチャ思考 先生:Kaz 米田タクラムと考える未来の文化創造に必要なアーキテクチャ思考 先生:Kaz 米田
タクラムと考える未来の文化創造に必要なアーキテクチャ思考 先生:Kaz 米田
schoowebcampus
 
Python-List comprehension
Python-List comprehensionPython-List comprehension
Python-List comprehension
Colin Su
 
200604material ozaki
200604material ozaki200604material ozaki
200604material ozaki
RCCSRENKEI
 
行列およびテンソルデータに対する機械学習(数理助教の会 2011/11/28)
行列およびテンソルデータに対する機械学習(数理助教の会 2011/11/28)行列およびテンソルデータに対する機械学習(数理助教の会 2011/11/28)
行列およびテンソルデータに対する機械学習(数理助教の会 2011/11/28)
ryotat
 
多チャンネルバイラテラルフィルタの高速化
多チャンネルバイラテラルフィルタの高速化多チャンネルバイラテラルフィルタの高速化
多チャンネルバイラテラルフィルタの高速化
Norishige Fukushima
 
Modul mesin bubut 7 (5)
Modul mesin bubut 7 (5)Modul mesin bubut 7 (5)
Modul mesin bubut 7 (5)
Eko Supriyadi
 
球面フィッティングの導出と実装
球面フィッティングの導出と実装球面フィッティングの導出と実装
球面フィッティングの導出と実装
j_rocket_boy
 
Introduction to-python
Introduction to-pythonIntroduction to-python
Introduction to-python
Aakashdata
 
Q1 Memory Fabric Forum: Memory expansion with CXL-Ready Systems and Devices
Q1 Memory Fabric Forum: Memory expansion with CXL-Ready Systems and DevicesQ1 Memory Fabric Forum: Memory expansion with CXL-Ready Systems and Devices
Q1 Memory Fabric Forum: Memory expansion with CXL-Ready Systems and Devices
Memory Fabric Forum
 
Transient three dimensional cfd modelling of ceilng fan
Transient three dimensional cfd modelling of ceilng fanTransient three dimensional cfd modelling of ceilng fan
Transient three dimensional cfd modelling of ceilng fan
Lahiru Dilshan
 
♥3♥مبادئ الفلسفة 101 بول كلينمان.pdf
♥3♥مبادئ الفلسفة 101 بول كلينمان.pdf♥3♥مبادئ الفلسفة 101 بول كلينمان.pdf
♥3♥مبادئ الفلسفة 101 بول كلينمان.pdf
khatab719
 
API Developer Training: Insights for Integrations
API Developer Training: Insights for IntegrationsAPI Developer Training: Insights for Integrations
API Developer Training: Insights for Integrations
JeremyOtt5
 
MemVerge: The Software Stack for CXL Environments
MemVerge: The Software Stack for CXL EnvironmentsMemVerge: The Software Stack for CXL Environments
MemVerge: The Software Stack for CXL Environments
Memory Fabric Forum
 
Hardware-assisted Isolated Execution Environment to run trusted OS and applic...
Hardware-assisted Isolated Execution Environment to run trusted OS and applic...Hardware-assisted Isolated Execution Environment to run trusted OS and applic...
Hardware-assisted Isolated Execution Environment to run trusted OS and applic...
Kuniyasu Suzaki
 
Introduction to python 3
Introduction to python 3Introduction to python 3
Introduction to python 3
Youhei Sakurai
 
IntelON 2021 Processor Benchmarking
IntelON 2021 Processor BenchmarkingIntelON 2021 Processor Benchmarking
IntelON 2021 Processor Benchmarking
Brendan Gregg
 
Franco y la guerra civil española1
Franco y la guerra civil española1Franco y la guerra civil española1
Franco y la guerra civil española1
Neil Jones
 
JSR381 Visual Recognition for Java.pdf
JSR381 Visual Recognition for Java.pdfJSR381 Visual Recognition for Java.pdf
JSR381 Visual Recognition for Java.pdf
Zoran Sevarac, PhD
 

Viewers also liked (20)

Research 101 - Paper Writing with LaTeX
Research 101 - Paper Writing with LaTeXResearch 101 - Paper Writing with LaTeX
Research 101 - Paper Writing with LaTeX
Jia-Bin Huang
 
美國研究所申請流程 (A Guide for Applying Graduate Schools in USA)
美國研究所申請流程 (A Guide for Applying Graduate Schools in USA)美國研究所申請流程 (A Guide for Applying Graduate Schools in USA)
美國研究所申請流程 (A Guide for Applying Graduate Schools in USA)
Jia-Bin Huang
 
How to Read Academic Papers
How to Read Academic PapersHow to Read Academic Papers
How to Read Academic Papers
Jia-Bin Huang
 
How to come up with new research ideas
How to come up with new research ideasHow to come up with new research ideas
How to come up with new research ideas
Jia-Bin Huang
 
What Makes a Creative Photograph?
What Makes a Creative Photograph?What Makes a Creative Photograph?
What Makes a Creative Photograph?
Jia-Bin Huang
 
Linear Algebra and Matlab tutorial
Linear Algebra and Matlab tutorialLinear Algebra and Matlab tutorial
Linear Algebra and Matlab tutorial
Jia-Bin Huang
 
Computer Vision Crash Course
Computer Vision Crash CourseComputer Vision Crash Course
Computer Vision Crash Course
Jia-Bin Huang
 
Toward Accurate and Robust Cross-Ratio based Gaze Trackers Through Learning F...
Toward Accurate and Robust Cross-Ratio based Gaze Trackers Through Learning F...Toward Accurate and Robust Cross-Ratio based Gaze Trackers Through Learning F...
Toward Accurate and Robust Cross-Ratio based Gaze Trackers Through Learning F...
Jia-Bin Huang
 
Transformation Guided Image Completion ICCP 2013
Transformation Guided Image Completion ICCP 2013Transformation Guided Image Completion ICCP 2013
Transformation Guided Image Completion ICCP 2013
Jia-Bin Huang
 
Saliency Detection via Divergence Analysis: A Unified Perspective ICPR 2012
Saliency Detection via Divergence Analysis: A Unified Perspective ICPR 2012Saliency Detection via Divergence Analysis: A Unified Perspective ICPR 2012
Saliency Detection via Divergence Analysis: A Unified Perspective ICPR 2012
Jia-Bin Huang
 
Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)
Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)
Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)
Jia-Bin Huang
 
Image Completion using Planar Structure Guidance (SIGGRAPH 2014)
Image Completion using Planar Structure Guidance (SIGGRAPH 2014)Image Completion using Planar Structure Guidance (SIGGRAPH 2014)
Image Completion using Planar Structure Guidance (SIGGRAPH 2014)
Jia-Bin Huang
 
Estimating Human Pose from Occluded Images (ACCV 2009)
Estimating Human Pose from Occluded Images (ACCV 2009)Estimating Human Pose from Occluded Images (ACCV 2009)
Estimating Human Pose from Occluded Images (ACCV 2009)
Jia-Bin Huang
 
Lecture 21 - Image Categorization - Computer Vision Spring2015
Lecture 21 - Image Categorization -  Computer Vision Spring2015Lecture 21 - Image Categorization -  Computer Vision Spring2015
Lecture 21 - Image Categorization - Computer Vision Spring2015
Jia-Bin Huang
 
Applying for Graduate School in S.T.E.M.
Applying for Graduate School in S.T.E.M.Applying for Graduate School in S.T.E.M.
Applying for Graduate School in S.T.E.M.
Jia-Bin Huang
 
Lecture 29 Convolutional Neural Networks - Computer Vision Spring2015
Lecture 29 Convolutional Neural Networks -  Computer Vision Spring2015Lecture 29 Convolutional Neural Networks -  Computer Vision Spring2015
Lecture 29 Convolutional Neural Networks - Computer Vision Spring2015
Jia-Bin Huang
 
Three Reasons to Join FVE at uiuc
Three Reasons to Join FVE at uiucThree Reasons to Join FVE at uiuc
Three Reasons to Join FVE at uiuc
Jia-Bin Huang
 
A Physical Approach to Moving Cast Shadow Detection (ICASSP 2009)
A Physical Approach to Moving Cast Shadow Detection (ICASSP 2009)A Physical Approach to Moving Cast Shadow Detection (ICASSP 2009)
A Physical Approach to Moving Cast Shadow Detection (ICASSP 2009)
Jia-Bin Huang
 
Jia-Bin Huang's Curriculum Vitae
Jia-Bin Huang's Curriculum VitaeJia-Bin Huang's Curriculum Vitae
Jia-Bin Huang's Curriculum Vitae
Jia-Bin Huang
 
UIUC CS 498 - Computational Photography - Final project presentation
UIUC CS 498 - Computational Photography - Final project presentation UIUC CS 498 - Computational Photography - Final project presentation
UIUC CS 498 - Computational Photography - Final project presentation
Jia-Bin Huang
 
Research 101 - Paper Writing with LaTeX
Research 101 - Paper Writing with LaTeXResearch 101 - Paper Writing with LaTeX
Research 101 - Paper Writing with LaTeX
Jia-Bin Huang
 
美國研究所申請流程 (A Guide for Applying Graduate Schools in USA)
美國研究所申請流程 (A Guide for Applying Graduate Schools in USA)美國研究所申請流程 (A Guide for Applying Graduate Schools in USA)
美國研究所申請流程 (A Guide for Applying Graduate Schools in USA)
Jia-Bin Huang
 
How to Read Academic Papers
How to Read Academic PapersHow to Read Academic Papers
How to Read Academic Papers
Jia-Bin Huang
 
How to come up with new research ideas
How to come up with new research ideasHow to come up with new research ideas
How to come up with new research ideas
Jia-Bin Huang
 
What Makes a Creative Photograph?
What Makes a Creative Photograph?What Makes a Creative Photograph?
What Makes a Creative Photograph?
Jia-Bin Huang
 
Linear Algebra and Matlab tutorial
Linear Algebra and Matlab tutorialLinear Algebra and Matlab tutorial
Linear Algebra and Matlab tutorial
Jia-Bin Huang
 
Computer Vision Crash Course
Computer Vision Crash CourseComputer Vision Crash Course
Computer Vision Crash Course
Jia-Bin Huang
 
Toward Accurate and Robust Cross-Ratio based Gaze Trackers Through Learning F...
Toward Accurate and Robust Cross-Ratio based Gaze Trackers Through Learning F...Toward Accurate and Robust Cross-Ratio based Gaze Trackers Through Learning F...
Toward Accurate and Robust Cross-Ratio based Gaze Trackers Through Learning F...
Jia-Bin Huang
 
Transformation Guided Image Completion ICCP 2013
Transformation Guided Image Completion ICCP 2013Transformation Guided Image Completion ICCP 2013
Transformation Guided Image Completion ICCP 2013
Jia-Bin Huang
 
Saliency Detection via Divergence Analysis: A Unified Perspective ICPR 2012
Saliency Detection via Divergence Analysis: A Unified Perspective ICPR 2012Saliency Detection via Divergence Analysis: A Unified Perspective ICPR 2012
Saliency Detection via Divergence Analysis: A Unified Perspective ICPR 2012
Jia-Bin Huang
 
Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)
Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)
Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)
Jia-Bin Huang
 
Image Completion using Planar Structure Guidance (SIGGRAPH 2014)
Image Completion using Planar Structure Guidance (SIGGRAPH 2014)Image Completion using Planar Structure Guidance (SIGGRAPH 2014)
Image Completion using Planar Structure Guidance (SIGGRAPH 2014)
Jia-Bin Huang
 
Estimating Human Pose from Occluded Images (ACCV 2009)
Estimating Human Pose from Occluded Images (ACCV 2009)Estimating Human Pose from Occluded Images (ACCV 2009)
Estimating Human Pose from Occluded Images (ACCV 2009)
Jia-Bin Huang
 
Lecture 21 - Image Categorization - Computer Vision Spring2015
Lecture 21 - Image Categorization -  Computer Vision Spring2015Lecture 21 - Image Categorization -  Computer Vision Spring2015
Lecture 21 - Image Categorization - Computer Vision Spring2015
Jia-Bin Huang
 
Applying for Graduate School in S.T.E.M.
Applying for Graduate School in S.T.E.M.Applying for Graduate School in S.T.E.M.
Applying for Graduate School in S.T.E.M.
Jia-Bin Huang
 
Lecture 29 Convolutional Neural Networks - Computer Vision Spring2015
Lecture 29 Convolutional Neural Networks -  Computer Vision Spring2015Lecture 29 Convolutional Neural Networks -  Computer Vision Spring2015
Lecture 29 Convolutional Neural Networks - Computer Vision Spring2015
Jia-Bin Huang
 
Three Reasons to Join FVE at uiuc
Three Reasons to Join FVE at uiucThree Reasons to Join FVE at uiuc
Three Reasons to Join FVE at uiuc
Jia-Bin Huang
 
A Physical Approach to Moving Cast Shadow Detection (ICASSP 2009)
A Physical Approach to Moving Cast Shadow Detection (ICASSP 2009)A Physical Approach to Moving Cast Shadow Detection (ICASSP 2009)
A Physical Approach to Moving Cast Shadow Detection (ICASSP 2009)
Jia-Bin Huang
 
Jia-Bin Huang's Curriculum Vitae
Jia-Bin Huang's Curriculum VitaeJia-Bin Huang's Curriculum Vitae
Jia-Bin Huang's Curriculum Vitae
Jia-Bin Huang
 
UIUC CS 498 - Computational Photography - Final project presentation
UIUC CS 498 - Computational Photography - Final project presentation UIUC CS 498 - Computational Photography - Final project presentation
UIUC CS 498 - Computational Photography - Final project presentation
Jia-Bin Huang
 
Ad

Similar to Writing Fast MATLAB Code (20)

ML-CheatSheet (1).pdf
ML-CheatSheet (1).pdfML-CheatSheet (1).pdf
ML-CheatSheet (1).pdf
KarroumAbdelmalek
 
Matlab Tutorial.ppt
Matlab Tutorial.pptMatlab Tutorial.ppt
Matlab Tutorial.ppt
RaviMuthamala1
 
An Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked ExamplesAn Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked Examples
eAssessment in Practice Symposium
 
Lines and planes in space
Lines and planes in spaceLines and planes in space
Lines and planes in space
Faizan Shabbir
 
matlab tutorial with separate function description and handson learning
matlab tutorial with separate function description and handson learningmatlab tutorial with separate function description and handson learning
matlab tutorial with separate function description and handson learning
vishalkumarpandey12
 
Matlab
MatlabMatlab
Matlab
Maria Akther
 
Matopt
MatoptMatopt
Matopt
Afaf Soumia Medjden
 
Matlab tips and tricks
Matlab tips and tricksMatlab tips and tricks
Matlab tips and tricks
Tariq kanher
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingMatlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processing
Dr. Manjunatha. P
 
matlab_tutorial.ppt
matlab_tutorial.pptmatlab_tutorial.ppt
matlab_tutorial.ppt
SudhirNayak43
 
matlab_tutorial.ppt
matlab_tutorial.pptmatlab_tutorial.ppt
matlab_tutorial.ppt
KrishnaChaitanya139768
 
matlab_tutorial.ppt
matlab_tutorial.pptmatlab_tutorial.ppt
matlab_tutorial.ppt
ManasaChevula1
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
Dun Automation Academy
 
Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4
Randa Elanwar
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.ppt
kebeAman
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
Vinay Kumar
 
Mbd dd
Mbd ddMbd dd
Mbd dd
Aditya Choudhury
 
Fundamentals of Image Processing & Computer Vision with MATLAB
Fundamentals of Image Processing & Computer Vision with MATLABFundamentals of Image Processing & Computer Vision with MATLAB
Fundamentals of Image Processing & Computer Vision with MATLAB
Ali Ghanbarzadeh
 
MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
AkashSingh728626
 
Matlab solved problems
Matlab solved problemsMatlab solved problems
Matlab solved problems
Make Mannan
 
Lines and planes in space
Lines and planes in spaceLines and planes in space
Lines and planes in space
Faizan Shabbir
 
matlab tutorial with separate function description and handson learning
matlab tutorial with separate function description and handson learningmatlab tutorial with separate function description and handson learning
matlab tutorial with separate function description and handson learning
vishalkumarpandey12
 
Matlab tips and tricks
Matlab tips and tricksMatlab tips and tricks
Matlab tips and tricks
Tariq kanher
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingMatlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processing
Dr. Manjunatha. P
 
Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4
Randa Elanwar
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.ppt
kebeAman
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
Vinay Kumar
 
Fundamentals of Image Processing & Computer Vision with MATLAB
Fundamentals of Image Processing & Computer Vision with MATLABFundamentals of Image Processing & Computer Vision with MATLAB
Fundamentals of Image Processing & Computer Vision with MATLAB
Ali Ghanbarzadeh
 
Matlab solved problems
Matlab solved problemsMatlab solved problems
Matlab solved problems
Make Mannan
 
Ad

More from Jia-Bin Huang (13)

How to write a clear paper
How to write a clear paperHow to write a clear paper
How to write a clear paper
Jia-Bin Huang
 
Single Image Super-Resolution from Transformed Self-Exemplars (CVPR 2015)
Single Image Super-Resolution from Transformed Self-Exemplars (CVPR 2015)Single Image Super-Resolution from Transformed Self-Exemplars (CVPR 2015)
Single Image Super-Resolution from Transformed Self-Exemplars (CVPR 2015)
Jia-Bin Huang
 
Real-time Face Detection and Recognition
Real-time Face Detection and RecognitionReal-time Face Detection and Recognition
Real-time Face Detection and Recognition
Jia-Bin Huang
 
Pose aware online visual tracking
Pose aware online visual trackingPose aware online visual tracking
Pose aware online visual tracking
Jia-Bin Huang
 
Face Expression Enhancement
Face Expression EnhancementFace Expression Enhancement
Face Expression Enhancement
Jia-Bin Huang
 
Image Smoothing for Structure Extraction
Image Smoothing for Structure ExtractionImage Smoothing for Structure Extraction
Image Smoothing for Structure Extraction
Jia-Bin Huang
 
Static and Dynamic Hand Gesture Recognition
Static and Dynamic Hand Gesture RecognitionStatic and Dynamic Hand Gesture Recognition
Static and Dynamic Hand Gesture Recognition
Jia-Bin Huang
 
Real-Time Face Detection, Tracking, and Attributes Recognition
Real-Time Face Detection, Tracking, and Attributes RecognitionReal-Time Face Detection, Tracking, and Attributes Recognition
Real-Time Face Detection, Tracking, and Attributes Recognition
Jia-Bin Huang
 
Estimating Human Pose from Occluded Images (ACCV 2009)
Estimating Human Pose from Occluded Images (ACCV 2009)Estimating Human Pose from Occluded Images (ACCV 2009)
Estimating Human Pose from Occluded Images (ACCV 2009)
Jia-Bin Huang
 
Information Preserving Color Transformation for Protanopia and Deuteranopia (...
Information Preserving Color Transformation for Protanopia and Deuteranopia (...Information Preserving Color Transformation for Protanopia and Deuteranopia (...
Information Preserving Color Transformation for Protanopia and Deuteranopia (...
Jia-Bin Huang
 
Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)
Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)
Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)
Jia-Bin Huang
 
Learning Moving Cast Shadows for Foreground Detection (VS 2008)
Learning Moving Cast Shadows for Foreground Detection (VS 2008)Learning Moving Cast Shadows for Foreground Detection (VS 2008)
Learning Moving Cast Shadows for Foreground Detection (VS 2008)
Jia-Bin Huang
 
Learning Moving Cast Shadows for Foreground Detection (VS 2008)
Learning Moving Cast Shadows for Foreground Detection (VS 2008)Learning Moving Cast Shadows for Foreground Detection (VS 2008)
Learning Moving Cast Shadows for Foreground Detection (VS 2008)
Jia-Bin Huang
 
How to write a clear paper
How to write a clear paperHow to write a clear paper
How to write a clear paper
Jia-Bin Huang
 
Single Image Super-Resolution from Transformed Self-Exemplars (CVPR 2015)
Single Image Super-Resolution from Transformed Self-Exemplars (CVPR 2015)Single Image Super-Resolution from Transformed Self-Exemplars (CVPR 2015)
Single Image Super-Resolution from Transformed Self-Exemplars (CVPR 2015)
Jia-Bin Huang
 
Real-time Face Detection and Recognition
Real-time Face Detection and RecognitionReal-time Face Detection and Recognition
Real-time Face Detection and Recognition
Jia-Bin Huang
 
Pose aware online visual tracking
Pose aware online visual trackingPose aware online visual tracking
Pose aware online visual tracking
Jia-Bin Huang
 
Face Expression Enhancement
Face Expression EnhancementFace Expression Enhancement
Face Expression Enhancement
Jia-Bin Huang
 
Image Smoothing for Structure Extraction
Image Smoothing for Structure ExtractionImage Smoothing for Structure Extraction
Image Smoothing for Structure Extraction
Jia-Bin Huang
 
Static and Dynamic Hand Gesture Recognition
Static and Dynamic Hand Gesture RecognitionStatic and Dynamic Hand Gesture Recognition
Static and Dynamic Hand Gesture Recognition
Jia-Bin Huang
 
Real-Time Face Detection, Tracking, and Attributes Recognition
Real-Time Face Detection, Tracking, and Attributes RecognitionReal-Time Face Detection, Tracking, and Attributes Recognition
Real-Time Face Detection, Tracking, and Attributes Recognition
Jia-Bin Huang
 
Estimating Human Pose from Occluded Images (ACCV 2009)
Estimating Human Pose from Occluded Images (ACCV 2009)Estimating Human Pose from Occluded Images (ACCV 2009)
Estimating Human Pose from Occluded Images (ACCV 2009)
Jia-Bin Huang
 
Information Preserving Color Transformation for Protanopia and Deuteranopia (...
Information Preserving Color Transformation for Protanopia and Deuteranopia (...Information Preserving Color Transformation for Protanopia and Deuteranopia (...
Information Preserving Color Transformation for Protanopia and Deuteranopia (...
Jia-Bin Huang
 
Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)
Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)
Enhancing Color Representation for the Color Vision Impaired (CVAVI 2008)
Jia-Bin Huang
 
Learning Moving Cast Shadows for Foreground Detection (VS 2008)
Learning Moving Cast Shadows for Foreground Detection (VS 2008)Learning Moving Cast Shadows for Foreground Detection (VS 2008)
Learning Moving Cast Shadows for Foreground Detection (VS 2008)
Jia-Bin Huang
 
Learning Moving Cast Shadows for Foreground Detection (VS 2008)
Learning Moving Cast Shadows for Foreground Detection (VS 2008)Learning Moving Cast Shadows for Foreground Detection (VS 2008)
Learning Moving Cast Shadows for Foreground Detection (VS 2008)
Jia-Bin Huang
 

Recently uploaded (20)

dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdfdp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdf
pravkumarbiz
 
FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025
Safe Software
 
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Intelli grow
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 
Making significant Software Architecture decisions
Making significant Software Architecture decisionsMaking significant Software Architecture decisions
Making significant Software Architecture decisions
Bert Jan Schrijver
 
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
 
What is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdfWhat is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdf
Varsha Nayak
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptxMOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
How to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdfHow to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdf
Creative Fosters
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 WebinarPorting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native BarcelonaOpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlowDevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?
felipeceotto
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
Migrating to Azure Cosmos DB the Right Way
Migrating to Azure Cosmos DB the Right WayMigrating to Azure Cosmos DB the Right Way
Migrating to Azure Cosmos DB the Right Way
Alexander (Alex) Komyagin
 
IBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - IntroductionIBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - Introduction
Gaurav Sharma
 
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI SearchAgentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Maxim Salnikov
 
Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)
VICTOR MAESTRE RAMIREZ
 
Step by step guide to install Flutter and Dart
Step by step guide to install Flutter and DartStep by step guide to install Flutter and Dart
Step by step guide to install Flutter and Dart
S Pranav (Deepu)
 
dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdfdp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdf
pravkumarbiz
 
FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025
Safe Software
 
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Intelli grow
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 
Making significant Software Architecture decisions
Making significant Software Architecture decisionsMaking significant Software Architecture decisions
Making significant Software Architecture decisions
Bert Jan Schrijver
 
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
 
What is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdfWhat is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdf
Varsha Nayak
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptxMOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
How to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdfHow to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdf
Creative Fosters
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 WebinarPorting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native BarcelonaOpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlowDevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?
felipeceotto
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
IBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - IntroductionIBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - Introduction
Gaurav Sharma
 
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI SearchAgentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Maxim Salnikov
 
Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)
VICTOR MAESTRE RAMIREZ
 
Step by step guide to install Flutter and Dart
Step by step guide to install Flutter and DartStep by step guide to install Flutter and Dart
Step by step guide to install Flutter and Dart
S Pranav (Deepu)
 

Writing Fast MATLAB Code

  • 1. Writing Fast MATLAB Code Jia-Bin Huang University of Illinois, Urbana-Champaign www.jiabinhuang.com [email protected]
  • 2. Resources • Techniques for Improving Performance by Mathwork • Writing Fast Matlab Code by Pascal Getreuer • Guidelines for writing clean and fast code in MATLAB by Nico Schlömer • http://www.slideshare.net/UNISTSupercomputingCenter/speeding- upmatlabapplications • http://www.matlabtips.com/
  • 3. Using the Profiler • Helps uncover performance problems • Timing functions: • tic, toc • The following timings were measured on - CPU i5 1.7 GHz - 4 GB RAM • http://www.mathworks.com/help/matlab/ref/profile.html
  • 4. Pre-allocation Memory 3.3071 s >> n = 1000; 2.1804 s2.5148 s
  • 5. Reducing Memory Operations >> x = 4; >> x(2) = 7; >> x(3) = 12; >> x = zeros(3,1); >> x = 4; >> x(2) = 7; >> x(3) = 12;
  • 7. Using Vectorization • Appearance • more like the mathematical expressions, easier to understand. • Less Error Prone • Vectorized code is often shorter. • Fewer opportunities to introduce programming errors. • Performance: • Often runs much faster than the corresponding code containing loops. See http://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
  • 8. Binary Singleton Expansion Function • Make each column in A zero mean >> n1 = 5000; >> n2 = 10000; >> A = randn(n1, n2); • See http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun- performance/ 0.2994 s 0.2251 s Why bsxfun is faster than repmat? - bsxfun handles replication of the array implicitly, thus avoid memory allocation - Bsxfun supports multi-thread
  • 9. Loop, Vector and Boolean Indexing • Make odd entries in vector v zero • n = 1e6; • See http://www.mathworks.com/help/matlab/learn_matlab/array-indexing.html • See Fast manipulation of multi-dimensional arrays in Matlab by Kevin Murphy 0.3772 s 0.0081 s 0.0130 s
  • 10. Solving Linear Equation System 0.1620 s 0.0467 s
  • 11. Dense and Sparse Matrices • Dense: 16.1332 s • Sparse: 0.0040 s More than 4000x faster! Useful functions: sparse(), spdiags(), speye(), kron(). 0.6424 s 0.1157 s
  • 12. Repeated solution of an equation system with the same matrix 3.0897 s 0.0739 s
  • 13. Iterative Methods for Larger Problems • Iterative solvers in MATLAB: • bicg, bicgstab, cgs, gmres, lsqr, minres, pcg, symmlq, qmr • [x,flag,relres,iter,resvec] = method(A,b,tol,maxit,M1,M2,x0) • source: Writing Fast Matlab Code by Pascal Getreuer
  • 14. Solving Ax = b when A is a Special Matrix • Circulant matrices • Matrices corresponding to cyclic convolution Ax = conv(h, x) are diagonalized in the Fourier domain >> x = ifft( fft(b) ./ fft(h) ); • Triangular and banded • Efficiently solved by sparse LU factorization >> [L,U] = lu(sparse(A)); >> x = U(Lb); • Poisson problems • See http://www.cs.berkeley.edu/~demmel/cs267/lecture25/lecture25.html
  • 16. Inlining Simple Functions 1.1942 s 0.3065 s functions are worth inlining: - conv, cross, fft2, fliplr, flipud, ifft, ifft2, ifftn, ind2sub, ismember, linspace, logspace, mean, median, meshgrid, poly, polyval, repmat, roots, rot90, setdiff, setxor, sortrows, std, sub2ind, union, unique, var y = medfilt1(x,5); 0.2082 s
  • 17. Using the Right Type of Data “Do not use a cannon to kill a mosquito.” double image: 0.5295 s uint8 image: 0.1676 s Confucius
  • 18. Matlab Organize its Arrays as Column-Major • Assign A to zero row-by-row or column-by-column >> n = 1e4; >> A = randn(n, n); 0.1041 s2.1740 s
  • 19. Column-Major Memory Storage >> x = magic(3) x = 8 1 6 3 5 7 4 9 2 % Access one column >> y = x(:, 1); % Access one row >> y = x(1, :);
  • 20. Copy-on-Write (COW) >> n = 500; >> A = randn(n,n,n); 0.4794 s 0.0940 s
  • 21. Clip values >> n = 2000; >> lowerBound = 0; >> upperBound = 1; >> A = randn(n,n); 0.0121 s0.1285 s
  • 22. Moving Average Filter • Compute an N-sample moving average of x >> n = 1e7; >> N = 1000; >> x = randn(n,1); 3.2285 s 0.3847 s
  • 23. Find the min/max of a matrix or N-d array >> n = 500; >> A = randn(n,n,n); 0.5465 s 0.1938 s
  • 24. Acceleration using MEX (Matlab Executable) • Call your C, C++, or Fortran codes from the MATLAB • Speed up specific subroutines • See http://www.mathworks.com/help/matlab/matlab_external/introducing-mex- files.html
  • 25. MATLAB Coder • MATLAB Coder™ generates standalone C and C++ code from MATLAB® code • See video examples in http://www.mathworks.com/products/matlab- coder/videos.html • See http://www.mathworks.com/products/matlab-coder/
  • 27. parfor for parallel processing • Requirements • Task independent • Order independent See http://www.mathworks.com/products/parallel-computing/
  • 28. Parallel Processing in Matlab • MatlabMPI • multicore • pMatlab: Parallel Matlab Toolbox • Parallel Computing Toolbox (Mathworks) • Distributed Computing Server (Mathworks) • MATLAB plug-in for CUDA (CUDA is a library that used an nVidia board) • Source: http://www-h.eng.cam.ac.uk/help/tpl/programs/Matlab/faster_scripts.html
  • 29. Resources for your final project • Awesome computer vision by Jia-Bin Huang • A curated list of computer vision resources • VLFeat • features extraction and matching, segmentation, clustering • Piotr's Computer Vision Matlab Toolbox • Filters, channels, detectors, image/video manipulation • OpenCV (MexOpenCV by Kota Yamaguchi) • General purpose computer vision library

Editor's Notes

  • #6: Repeatedly expanding the size of an array over time, (for example, adding more elements to it each time through a programming loop), can adversely affect the performance of your program. This is because 1) MATLAB has to spend time allocating more memory each time you increase the size of the array. 2) This newly allocated memory is likely to be noncontiguous, thus slowing down any operations that MATLAB needs to perform on the array.