% Script Bench1
% For a vector x, how expensive are sqrt(x) and cos(x) compared to y = scalar*x?
clc
% length of x...
n = 1000000;
% Clock granularity in milleseconds so this parameter ensures that
% we apply tic 'n toc to a substantial calculation...
nRepeat = 100;
% Sample data..
x = rand(n,1);
a = rand(1);
% Time y = scalar*x ...
tic
for k=1:nRepeat
y = a*x;
end
t1 = toc;
% Time y = sqrt(x) ...
tic
for k=1:nRepeat
y = sqrt(x);
end
t2 = toc;
% Time y = cos(x) ...
tic
for k=1:nRepeat
y = cos(x);
end
t3 = toc;
clc
fprintf('x = rand(%1d)\n',n)
fprintf('Time a*x = %5.2f\n',t1)
fprintf('Time sqrt(x) / Time a*x = %5.2f\n',t2/t1)
fprintf('Time cos(x) / Time a*x = %5.2f\n',t3/t1)