Вы находитесь на странице: 1из 29

MATLAB for Engineers:

Speeding up your code


Alistair Johnson
Adapted from slides by Violeta Monasterio

31st May 2012

Centre for Doctoral Training in Healthcare Innovation
Institute of Biomedical Engineering
Department of Engineering Science
University of Oxford

Supported by the RCUK Digital Economy Programme grant number EP/G036861/1
Speeding up your code
Why do you care?
Downtime slows down your research
Speeding up your code
Easy:
1. Preallocate
2. Vectorize

Harder:
3. Clever code
4. Parallelization
How to assess code's speed
Before we start optimizing your code, we
have to figure out how to time it!

Two main methods of timing code:
1. tic and toc
2. Profiler ( profile viewer )

tic and toc
Quick calls to see how fast code runs
First call tic;
Then calling toc; outputs the elapsed time in
seconds
ex6_1a.m ex6_1b.m
Profiler
Sophisticated interface for seeing where
bottlenecks in your code are

Uses profile command to generate timings

Easy to use GUI... we will see this later
Speeding up your code

Easy:
1. Preallocate
2. Vectorize
What is pre-allocation?
Each variable requires continuous memory

Pre-allocation gives each variable the space
it needs BEFORE you fill it with data
Preallocation
Every variable has a
"pointer" - which tells it the
variable's location in
memory

If you don't preallocate,
MATLAB must repeat:
1. Creating the pointer
2. Finding a continuous
section of memory
Wikimedia commons, released under GFDL:
http://en.wikipedia.org/wiki/File:Pointers.svg

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:1000000
x(k) = x(k-1) + 5;
end
0.085
seconds
0.305
seconds
Profiler
Preallocation what if the final size
can vary?
3 options:
don't pre-allocate (bad)

Preallocation
this code could be even faster!
block pre-allocate fully pre-allocate
Vectorization
To vectorize a computation means to
replace serial operations (loops) with
vector operations

MATLAB is a vectorized language (MATrix
LABoratory) - it knows how to handle
matrix multiplications the best






Vectorization
Example vectorization:
ex6_3.m




Vectorization
Most of the time this is easy! The main trick is to
constantly think: "can I vectorize this?"

Helpful functions! (tutorials to follow)
bsxfun
repmat
filter
cellfun
arrayfun
structfun





bsxfun
Most commonly, you will use this to calculate
something using a vector, across a matrix, e.g.:


More generally, this function applies a function
using an N dimensional matrix across an N+1
dimensional matrix





repmat
In a nutshell:
repmat duplicates a vector across a given
dimension
Practice! Open exercise6_1.m
"Normalize" each column in a matrix of data, this
involves:
a. Subtracting each column by its mean
b. Dividing each column by its standard deviation

Tips:
a. mean(data,1) calculates column means
b. std(data,[],1) calculates column standard
deviations
c. Generate your data like this:






Answers
filter
filter implements a 1-D filter
Syntax:
y = filter(B,A,INPUT);

Example transfer function with syntax:
1*y[n] = 2*x[n] + 3*x[n-1] - 4*y[n-1];
y = filter([2,3],[1,4], INPUT);

Quick exercise - exercise6_2.m


Exercise: vectorizing a loop that creates a
vector whose elements depend on the
previous element
Example: vectorizing a loop that creates a
vector whose elements depend on the
previous element
L = 1000;
A = filter([1],[1 -2],ones(1,L+1));
Solution:
The FUN functions!
arrayfun, cellfun, structfun
Used to evaluate a function across an array of
something

Let's look at ex6_5_funs.m





Speeding up your code




Harder:
3. Clever code
4. Parallelization
Example cleverness
AUROC



Example cleverness
AUROC



How to handle the "hard" part
Clever code is case-specific, but always keep
these things in mind:
a. Less operations in a loop == faster code
b. Built-in MATLAB functions == faster code
c. Less loops == faster code
d. Nested for loops == probably vectorizable
e. Using more memory ~~ using less time





Practice time: QRS detector
rpeakdetect_spe.m

To get you started - let's use reports!

I have added in TODO comments where
the code must be vectorized

Вам также может понравиться