Contents
A06A.m - GOOD - Using a for loop to calculate N values of y=sin(t)
N = 1e8; % set the number of points to 10^8 tic; % start timer to measure computation time i = 0; % initialize counter for t = linspace(0, 2*pi, N) % start of for loop i = i + 1; % increment counter y(i) = sin(t); % evaluate sin(t) end % end of for loop tElapsed = toc; % measure elapsed time since timer started % display elapsed time to command window fprintf('elapsed time = %.3g seconds\n', tElapsed);
This example uses a for loop to calculate y=sin(t) at N values of t. It is a nice example of how you could conceptually use a for loop, but it turns out to be incredibly inefficient in Matlab. Run this code and see how long it takes to execute on your computer. Compare your result with the more optimized codes in examples L2ex6B.m and L2ex6C.m.
The execution time is measured using the tic and toc commands. tic starts the internal timer and toc reads the elapsed time since the last tic command.
A06B.m - BETTER - Preallocating the y array speeds things up
N = 1e8; % set the number of points to 10^8 tic; % start timer to measure computation time y = zeros(1,N); % ***** Added this statement to pre-allocate y array i = 0; % initialize counter for t = linspace(0, 2*pi, N) % start of for loop i = i + 1; % increment counter y(i) = sin(t); % evaluate sin(t) end % end of for loop tElapsed = toc; % measure elapsed time since timer started % display elapsed time to command window fprintf('elapsed time = %.3g seconds\n', tElapsed);
This program is exactly the same as L2ex6A.m except that the y array has been preallocated using the zeros() command. zeros(M,N) fills an MxN array with zeros. Since we want to store a list of N numbers, we use the command y = zeros(1,N);. Doing this preallocates space in memory to store the y values. Without preallocating memory, Matlab must search through memory to find an empty space to store the next number in the array every time the array is updated in the for loop. This can be very time consuming especially if N = 10^8. Preallocating reserves a chunk of memory all at once before the for loop is executed.
A06C.m - BEST - Using vectorized functions speeds things up even more
N = 1e8; % set the number of points to 10^8 tic; % start timer to measure computation time t = linspace(0, 2*pi, N); % define an array of N equally spaced t values y = sin(t); % evaluate sin(t) tElapsed = toc; % measure elapsed time since timer started % display elapsed time to command window fprintf('elapsed time = %.3g seconds\n', tElapsed);
Finally we show how to do the calculation without any for loops at all! We first define an array t that stores the 100 million t values. We use the linspace() command so we don't have to calculate the step spacing by hand (it figures it out for us). Next we calculate a y value for every value in the t array using the simple command y = sin(t);. Not only is this notation easier to read and more compact than the for loop examples above, it should also run faster! Most Matlab functions such as sin, cos, log, etc. are vectorized. Vectorization is a method of dividing up computations and distributing them across multiple processors. It uses "division of labor" to greatly speed up many calculations. We strongly advise using vectorized Matlab functions whenever possible.