function lec01timer

nn = 510:515;
for i = 1:length(nn)

n = nn(i);
A = rand(n);
B = rand(n);

fprintf('\n-- %d --\n', n);
T(i,1) = time_matmul(@matmul1, 'Standard', 100, A, B);
T(i,2) = time_matmul(@matmul2, 'Dot product', 1, A, B);
T(i,3) = time_matmul(@matmul3, 'Outer product', 1, A, B);
T(i,4) = time_matmul(@matmul4, 'Col-oriented', 20, A, B);
T(i,5) = time_matmul(@matmul5, 'Row-oriented', 20, A, B);

end

names = {'Standard', 'Dot product', 'Outer product', ...
'Column-oriented', 'Row-oriented'};
fp = fopen('lec01timing.tex', 'w');
fprintf(fp, '\\begin{tabular}{r|llllll}\n');
fprintf(fp, '& %d ', nn);
for i = 1:length(names)
fprintf(fp, '\\\\ \\hline \n');
fprintf(fp, '%s ', names{i});
fprintf(fp, '& %g ', T(:,i));
end
fprintf(fp, '\n\\end{tabular}\n');
fclose(fp);

function t = time_matmul(mulf, tag,trials,A,B)

tic;
for trial = 1:trials
mulf(A,B);
end
t = 1000*toc/trials;
fprintf('%s:\t%g ms\n', tag, t);

function C = matmul1(A,B)

C = A*B;

function C = matmul2(A,B)

n = length(A);
C = zeros(n);
for j = 1:n
for i = 1:n
C(i,j) = A(i,:)*B(:,j);
end
end

function C = matmul3(A,B)

n = length(A);
C = zeros(n);
for k = 1:n
C = C + A(:,k)*B(k,:);
end

function C = matmul4(A,B)

n = length(A);
C = zeros(n);
for j = 1:n
C(:,j) = A*B(:,j);
end

function C = matmul5(A,B)

n = length(A);
C = zeros(n);
for i = 1:n
C(i,:) = A(i,:)*B;
end