speeding up your code
TRANSCRIPT
-
8/12/2019 Speeding Up Your Code
1/29
MATLAB for Engineers:Speeding up your code
Alistair JohnsonAdapted from slides by Violeta Monasterio
31st May 2012
Centre for Doctoral Training in Healthcare Innovation
Institute of Biomedical Engineering
Department of Engineering ScienceUniversity of Oxford
Supported by the RCUK Digital Economy Programme grant number EP/G036861/1
-
8/12/2019 Speeding Up Your Code
2/29
Speeding up your code
Why do you care?
Downtime slows down your research
-
8/12/2019 Speeding Up Your Code
3/29
Speeding up your code
Easy:
1. Preallocate2. Vectorize
Harder:
3. Clever code4. Parallelization
-
8/12/2019 Speeding Up Your Code
4/29
How to assess code's speed
Before we start optimizing your code, wehave to figure out how to time it!
Two main methods of timing code:
1. ticand toc2. Profiler (profile viewer)
-
8/12/2019 Speeding Up Your Code
5/29
tic and toc
Quick calls to see how fast code runs
First call tic;
Then calling toc;outputs the elapsed time inseconds
ex6_1a.m ex6_1b.m
-
8/12/2019 Speeding Up Your Code
6/29
Profiler
Sophisticated interface for seeing wherebottlenecks in your code are
Uses profilecommand to generate timings
Easy to use GUI... we will see this later
-
8/12/2019 Speeding Up Your Code
7/29
Speeding up your code
Easy:
1.
Preallocate2. Vectorize
-
8/12/2019 Speeding Up Your Code
8/29
What is pre-allocation?
Each variable requires continuous memory
Pre-allocation gives each variable the spaceit needs BEFOREyou fill it with data
-
8/12/2019 Speeding Up Your Code
9/29
Preallocation
Every variable has a"pointer" - which tells it thevariable's location inmemory
If you don't preallocate,
MATLAB must repeat:1. Creating the pointer2. Finding a continuous
section of memory
Wikimedia commons, released under GFDL:http://en.wikipedia.org/wiki/File:Pointers.svg
http://en.wikipedia.org/wiki/File:Pointers.svg -
8/12/2019 Speeding Up Your Code
10/29
Preallocation
Useful functions: cell, zeros, ones
x = 0;for k = 2:1000000
x(k) = x(k-1) + 5;end
x = zeros(1, 1000000);for k = 2:1000000x(k) = x(k-1) + 5;
end
0.085
seconds
0.305
seconds
-
8/12/2019 Speeding Up Your Code
11/29
Profiler
-
8/12/2019 Speeding Up Your Code
12/29
Preallocation what if the final sizecan vary?
3 options: don't pre-allocate (bad)
-
8/12/2019 Speeding Up Your Code
13/29
Preallocation
this code could be even faster!
block pre-allocate fully pre-allocate
-
8/12/2019 Speeding Up Your Code
14/29
Vectorization
To vectorize a computation means toreplace serial operations (loops) withvector operations
MATLAB is a vectorized language (MATrixLABoratory) - it knows how to handlematrix multiplications the best
-
8/12/2019 Speeding Up Your Code
15/29
Vectorization
Example vectorization: ex6_3.m
-
8/12/2019 Speeding Up Your Code
16/29
Vectorization
Most of the time this is easy! The main trick is toconstantly think: "can I vectorize this?"
Helpful functions! (tutorials to follow)
bsxfun repmat filter cellfun arrayfun structfun
-
8/12/2019 Speeding Up Your Code
17/29
bsxfun
Most commonly, you will use this to calculatesomething using a vector, across a matrix, e.g.:
More generally, this function applies a functionusing an N dimensional matrix across an N+1dimensional matrix
-
8/12/2019 Speeding Up Your Code
18/29
repmat
In a nutshell:
repmat duplicates a vector across a givendimension
-
8/12/2019 Speeding Up Your Code
19/29
Practice! Open exercise6_1.m
"Normalize" each column in a matrix of data, thisinvolves:a. Subtracting each column by its meanb. Dividing each column by its standard deviation
Tips:
a. mean(data,1) calculates column meansb. std(data,[],1) calculates column standard
deviationsc. Generate your data like this:
-
8/12/2019 Speeding Up Your Code
20/29
Answers
-
8/12/2019 Speeding Up Your Code
21/29
-
8/12/2019 Speeding Up Your Code
22/29
Exercise: vectorizing a loop that creates avector whose elements depend on the
previous element
-
8/12/2019 Speeding Up Your Code
23/29
Example: vectorizing a loop that creates avector whose elements depend on the
previous element
L = 1000;A = filter([1],[1 -2],ones(1,L+1));
Solution:
-
8/12/2019 Speeding Up Your Code
24/29
The FUN functions!
arrayfun, cellfun, structfun Used to evaluate a function across an array of
something
Let's look at ex6_5_funs.m
-
8/12/2019 Speeding Up Your Code
25/29
Speeding up your code
Harder:
3. Clever code4. Parallelization
-
8/12/2019 Speeding Up Your Code
26/29
Example cleverness
AUROC
-
8/12/2019 Speeding Up Your Code
27/29
Example cleverness
AUROC
-
8/12/2019 Speeding Up Your Code
28/29
How to handle the "hard" part
Clever code is case-specific, but always keepthese things in mind:a. Less operations in a loop == faster codeb. Built-in MATLAB functions == faster code
c. Less loops == faster coded. Nested for loops == probably vectorizablee. Using more memory ~~ using less time
-
8/12/2019 Speeding Up Your Code
29/29
Practice time: QRS detector
rpeakdetect_spe.m
To get you started - let's use reports!
I have added in TODO comments wherethe code must be vectorized