CZ1102 Computing & Problem Solving Lecture 10
CZ1102 Computing & Problem Solving Lecture 10
Week 12
By JI, Hui
Based on “Writing Fast MATLAB Code” by Pascal Getreuer
Introduction
• Matlab is a interpreted language, meaning it
focuses on the ease of development with
language flexibility, interactive debugging, and
other conveniences
• It lacks the performance of those compiled
languages like C and C++. While Matlab may
not be as fast as C, there are ways to bring it
closer.
Organization of your project
• Use a separate folder for each project.
• Write comments, especially write the help
comment for functions
• Save frequent console commands as a script
Commends for helping measuring
efficiency
• use the tic/toc stopwatch timer
– tic Start a stopwatch timer
– toc Read the stopwatch timer
>> tic;sin(rand(1,10^6));toc
Elapsed time is 0.055057 seconds.
• Do not use input tic/toc in different prompts, it will
count the typing time
• Using it in a single line or use it in script
>> tic;
>> toc
Elapsed time is 0.743795 seconds.
One way for array allocation
• Dynamic allocation, that is, dynamically
augment rows and columns of arrays
– e.g.
>> a=2
a =
2
>> a(2,6)=1
a =
2 0 0 0 0 0
0 0 0 0 0 1
(cont’)
– Matlab will automatically resize the matrix
• Internally, the matrix data memory must be reallocated
with larger size.
• The overhead (cost) can be significant if the matrix is
resized repeatedly, e.g. like within a loop
• It takes 1.2 seconds in a computer with core i5 CPU
clear a b
tic
a(1) = 1; b(1) = 0;
for k = 2:30000
a(k) = 0.99803 * a(k‐1)‐ 0.06279 * b(k‐1);
b(k) = 0.06279 * a(k‐1) + 0.99803 * b(k‐1);
end;
toc
Another way for array allocation
• Preallocate the matrix with the zeros
commands to avoid frequent reallocations
– e.g. It takes only 0.009514 seconds to run the
following code, 150 times faster than dynamic
allocation
tic;a = zeros(1,30000); % Preallocation
b = zeros(1,30000);
a(1) = 1;b(1) = 0;
for k = 2:30000
a(k) = 0.99803 * a(k‐1)‐0.06279 * b(k‐1);
b(k) = 0.06279 * a(k‐1) + 0.99803 * b(k‐1);
end;toc
What if the final array size can vary?
• Making the trade‐off between storage
efficiency and computing efficiency.
– Pre‐allocate a larger array and trim extra zeros.
– e.g. 3.8525 secs vs 0.0213 secs
a = zeros(1,50000); % Preallocate
count=0;
for k = 1:50000
v = exp(rand*rand);
if v > 0.5 % Conditionally add to array
count = count + 1;
a(count) = v;
end;end
a = a(1:count);
Vecterization
• A computation is vectorized by taking
advantage of vector operations.
• A variety of programming situations can be
vectorized, and often improving speed to 10
times faster or even better.
• Vectorization is one of the most general and
effective techniques for writing fast Matlab
codes.
Vectorized Computations
• Many standard mathematical functions are
“vectorized“ in Matlab
– they can operate on an array as if the function had
been applied individually to every element.
>> sqrt([1,4;9,16]) >> abs([0,1,2,‐5,‐6,‐7])
ans = ans =
1 2 0 1 2 5 6 7
3 4
One example of vecterized
computation
• Problem: Find the min distance between a set
of points and the origin
– Non‐vecterized code
function d = minDistance1(x,y,z)
% Find the min distance between a set of points and the origin
nPoints = length(x);
d = zeros(nPoints,1); % Preallocate
for k = 1:nPoints % Compute distance for every point
d(k) = sqrt(x(k)ˆ2 + y(k)ˆ2 + z(k)ˆ2);
end
d = min(d); % Get the minimum distance
(cont’)
– Vecterized code
function d = minDistance2(x,y,z)
% Find the min distance between a set of points and the origin
d = sqrt(x.ˆ2 + y.ˆ2 + z.ˆ2); % Compute distance for every point
d = min(d); % Get the minimum distanc
– The comparison: vecterized code 30 times faster
>> feature accel off; %For Matlab 7
>> x=rand(1,30000);y=rand(1,30000);z=rand(1,30000);
>> tic;minDistance2(x,y,z);toc;
Elapsed time is 0.002272 seconds.
>> tic;minDistance1(x,y,z);toc
Elapsed time is 0.060647 seconds.
Some useful function supporting
vecterization
• Min
• max
• sum,
• cumsum,
• prod,
• cumprod
• sort
• diff
Vectorized Logic
• Bottleneck code often involves conditional
logic
• Like computations, Matlab's logic operators
are also vectorized:
>> [1,5,3] < [2,2,4]
ans =
1 0 1
• Two arrays are compared per‐element. Logic
operations return logical arrays with binary values.
Three powerful commands for logical
arrays
• find: Find indices of nonzero elements
>> find([1,5,3] < [2,2,4])
ans =
1 3
• any: True if any element of a vector is nonzero
(or per column for a matrix)
>> any([1,5,3] < [2,2,4])
ans =
1
(cont’)
• all: True if all elements of a vector are nonzero
(or per column for a matrix)
>> all([1,5,3] < [2,2,4])
ans =
• Vecterized logic also works for matrices
>> find(eye(3) == 1)
ans =
1
5
9
Example 1
• Problem: remove negative elements from the
array
– V1: Non‐vecterized code
y =x; %Pre‐allocate
count=0;
for k=1:length(x),
if (x(k) >=0)
count =count+1;
y(count)=x(k);
end;
end;
y=y(1:count);
(cont’)
• V2: Vecterized code
idx= find(x>0); % Find elements that are non‐positive
y = x(idx);
• V3: Further streamlined version
y=x(find(x>0));
• Time comparison in seconds
– V1 : V2 : V3 = 0.04 : 0.0031 : 0.0010
Example 2
• Problem: piece‐wise function
– e.g. considering sinc function
sin( x ) / x, x 0
sinc( x )
1, x0
– Vecterized code
function y = sinc(x)
% Computes the sinc function per‐element for a set of x values.
y = ones(size(x)); % Set y to all ones, sinc(0) = 1
i = find(x ˜= 0); % Find nonzero x values
y(i) = sin(x(i)) ./ x(i); % Compute sinc where x ˜= 0
Referencing operations
• Referencing in Matlab is varied and powerful
enough.
• Good understanding of referencing enables
vectorizing a broader range of programming
situations.
Subscripts vs. Indices
• Subscripts are the most common method
used to refer to matrix elements, for example,
A(3,9) refers to row 3, column 9.
• Indices are an alternative referencing method
for matrix by viewing it as a vector
– An index refers to an element's position in this
one‐dimensional vector
– For a 10‐by‐10 matrix, A(83) also refers A(3,9)
(con’t)
• The matrix is stored in a column‐major order
– it means elements along a column are sequential
in memory while elements along a row are further
apart
– Thus, Scanning down columns promotes cache
efficiency
• It is faster to scan down columns than over
rows
Logical indexing
• Given a logical array mask with the same size
as A
– A(mask) refers to all elements of A where the
corresponding element of mask is 1 (true).
– It is equivalent to A(find(mask==1))
– Example: what the following code does
>>A(abs(A) < 1e‐3) = 0
Deleting Sub‐matrices with [ ]
• Elements in a matrix can be deleted by
assigning the empty matrix.
– A(2,:) = [ ] deletes the second row of A
– A([3,5]) = [] deletes the element A(3,5) from A and
reshape A to a vector
– Deletions like A(2,1) = [ ] is illegal.