% ShowTicToc
% Using tic and toc to benchmark a computation.
% Sample computation: setting up a length-N vector of random numbers.
clc
clear
N = 10000;
% Method 1. Component-by-Component, no pre-allocation of memory via
% zeros...
tic
for k=1:N
    x(k)  = rand(1);
end
Method1 = toc;
fprintf('Method 1 = %8.5f\n',Method1)
% Method 2. Component-by-Component, with pre-allocation of memory via
% zeros...
tic
y = zeros(1,N);
for k=1:N
    y(k) = rand(1);
end
Method2 = toc;
fprintf('Method 2 = %8.5f\n',Method2)
% Method 3. Vectorized...
tic
z = rand(1,N);
Method3 = toc;
fprintf('Method 3 = %8.5f\n',Method3)
% Method 3 again with more reliable benchmarking...
tic
nRepeat = 1000;
for i=1:nRepeat
    z = rand(1,N);
end
time = toc;
Method3Again = time/nRepeat;
fprintf('Method 3 = %8.5f\n',Method3Again)
