% OperationsTime.m
% Speeds of addition, subtraction, multiplication, & division in MATLAB.
% Result: No noticable difference in the speeds of 
% addition, subtraction, and multiplication.
% However division is about 50% slower.

% K. Ming Leung, 02/20/04
clear all;
format short;
N = 100000;
repeat = 100;

a = [(1:N)' rand(N,1)];             % Integer | floating point data
b = [(1:N)' rand(N,1)];             % Integer | floating point data

for m = 1:2   % integer and then floating point data
n = 1;

% Addition
t0 = cputime;
for i = 1:repeat
    result = a+b;
end
time = cputime - t0;
RESULT(n,m) = time;
n = n+1;

% Subtraction
t0 = cputime;
for i= 1 :repeat
    subtraction = a(:,m)-b(:,m);
end
time = cputime - t0;
RESULT(n,m) = time;
n = n+1;

% Multiplication 
t0 = cputime;
for i = 1:repeat
    result = a(:,m).*b(:,m);
end
time = cputime - t0;
RESULT(n,m) = time;
n = n+1;

% Division
t0 = cputime;
for i = 1:repeat
    result = a(:,m)./b(:,m);
end
time = cputime - t0;
RESULT(n,m) = time;
end

disp('   int      float');
disp(RESULT);
