clear all;

n = 2000;
a = rand(n, 1);
b = rand(n, 1);
c = rand(n, 1);
d = rand(n, 1);
e = zeros(n, n);

tic; % (1)
for i = 1 : n
	for j = 1 : n
		e(i, j) = sin((a(i) - b(j)) * (c(i) + d(j)));
	end
end
toc;
fprintf('%d %f\n', n, sum(e(:)) / n^2);

tic; % (2)
for j = 1 : n
	for i = 1 : n
		e(i, j) = sin((a(i) - b(j)) * (c(i) + d(j)));
	end
end
toc;
fprintf('%d %f\n', n, sum(e(:)) / n^2);

tic; % (3)
for i = 1 : n
	e(i, :) = sin((a(i) - b(:)) .* (c(i) + d(:)));
end
toc;
fprintf('%d %f\n', n, sum(e(:)) / n^2);

tic; % (4)
for j = 1 : n
	e(:, j) = sin((a(:) - b(j)) .* (c(:) + d(j)));
end
toc;
fprintf('%d %f\n', n, sum(e(:)) / n^2);
