Time results
Out of interest, I ran through the functions by Ripley, Hielor, Hlynkacg, francisdrake and myself three times and checked the time used by each function while using 3.6 million + 1 different angles between 0 and 360 degrees in MATLAB (every 0.0001 degree).
I copied each code and translated it to MATLAB. Note that Hlynkacg and francisdrake had typos in their code (I fixed them), both assuming the boundrary between left and front was at 345 degrees (it is at 315 degrees), and Hlynkacg's code also ignored values at exactly 315, 45, 135 and 225 degrees, and therefore returning 'back' for those four angles.
I did not discover francisdrake's error until after I had recorded the time, but I did another run afterwards, and the results were comparable.
Here are the results (all numbers are in seconds):
User | First round | Second round | Third round | Average
Ripley | 20.4867 | 20.1066 | 19.9343 | 20.1759
Hielor | 20.4928 | 20.3260 | 20.3308 | 20.3832
Hlynkacg | 34.5959 | 34.8855 | 34.3873 | 34.6229
francisdrake | 34.7324 | 35.0789 | 34.1148 | 34.6420
asbjos | 46.5920 | 46.4698 | 43.8758 | 45.6459
As we can see from the results, Ripley and Hielor had practically the same code (Hielor subtracted 45 from the angle), and therefore the same time, with Hielor only being 1.03 % slower than Ripley.
Hlynkacg and francisdrake both got close to 35 seconds, even though Hlynkacg had almost the same code as Ripley. It seems like the difference is in Hlynkacg using 'return' in each if-else-statement. I ran Hlynkacg's code afterwards without 'return'-lines, and the time then went down to 20.2812 s, just as Ripley and Hielor. I then inserted the 'return'-lines again, and the time went up to normal again.
My own code was the loser in this test, using an average of 46 seconds, more than double the time of Ripley's and Hielor's code. I suspect the fault is in the array of names. I removed the array, so that the function would return 1 (0 in C++) for front, 2 for right, etc., and the time then dropped down to only 6.0934 seconds (a third of Ripley's and Hielor's). But this is unfair, as the code only needs to return a number, and not a word. I rewrote Ripley's code so that it would return 1 for front, 2 for right, etc., and then the time dropped to only 4.8464 seconds.
So it really seems like the simplest is the fastest (naturally), both for humans and computers.
For anyone interested, here is the code in MATLAB:
runAll.m (the script which ran each function 3 600 001 times):
Code:
% http://orbiter-forum.com/showthread.php?p=504866
% Run for angles between 0 and 360 degrees, checking every 10^(-4)
% [= 0.0001] angles, to get more reliable results.
% 'tic' starts (or resets if it's used multiple times) a timer, 'toc'
% returns the current timer value.
% 'clear' removes all values in memory.
% This code assumes that all the functions are correct. It does not check
% if the output value of each function is the correct answer.
% Every for-loop uses a different variable to make sure that no loops
% benefit from previous runs.
% The program was closed and reopened for every run.
value = 0;
% Ripley:
clear
tic
for i = 0 : 10^(-4) : 360
value = Ripley (i);
end
disp(toc);
disp('^ Ripley');
value = 0;
disp(' ');
% Hielor:
clear
tic
for j = 0 : 10^(-4) : 360
value = Hielor (j);
end
disp(toc);
disp('^ Hielor');
value = 0;
disp(' ');
% Hlynkacg:
clear
tic
for k = 0 : 10^(-4) : 360
value = Hlynkacg (k);
end
disp(toc);
disp('^ Hlynkacg');
value = 0;
disp(' ');
% francisdrake:
clear
tic
for l = 0 : 10^(-4) : 360
value = francisdrake (l);
end
disp(toc);
disp('^ francisdrake');
value = 0;
disp(' ');
% asbjos
clear
tic
for m = 0 : 10^(-4) : 360
value = asbjos (m);
end
disp(toc);
disp('^ asbjos');
value = 0;
disp(' ');
% end of file
Ripley.m (Ripley's function, converted to MATLAB):
Code:
function direction = Ripley (angle)
% http://orbiter-forum.com/showthread.php?p=504787&postcount=2
if angle > 45 && angle <= 135
direction = 'right';
elseif angle > 135 && angle <= 225
direction = 'down';
elseif angle > 225 && angle <= 315
direction = 'left';
else
direction = 'front';
end
end
Hielor.m (Hielor's function, converted to MATLAB):
Code:
function direction = Hielor (angle)
% http://orbiter-forum.com/showthread.php?p=504789&postcount=4
degree = angle - 45;
if degree >= 0 && degree < 90
direction = 'right';
elseif degree >= 90 && degree < 180
direction = 'back';
elseif degree >= 180 && degree < 270
direction = 'left';
else
direction = 'front';
end
end
Hlynkacg.m (Hlynkacg's function, converted to MATLAB):
Code:
function direction = Hlynkacg (angle)
% http://orbiter-forum.com/showthread.php?p=504800&postcount=7
% Hlyankacg did a mistake, by assuming the boundrary between left and
% front was at 345, while it is at 315 degrees.
% Hlyankacg's code also did not think about values at exactly 45, 135,
% 225 or 315 degrees, which would all return 'back'. The correct code
% is to check if an angle is '<=', not '<'
if angle >= 315 || angle < 45
direction = 'front';
return
elseif angle >= 45 && angle < 135
direction = 'right';
return
elseif angle <= 315 && angle > 225
direction = 'left';
return
else
direction = 'back';
return
end
end
francisdrake.m (francisdrake's function, converted to MATLAB):
Code:
function direction = francisdrake (angle)
% http://orbiter-forum.com/showthread.php?p=504844&postcount=8
% francisdrake did a mistake, by assuming the boundrary between left and
% front was at 345, while it is at 315 degrees.
if angle > 315
direction = 'front';
return
end
if angle > 225
direction = 'left';
return
end
if angle > 135
direction = 'back';
return
end
if angle > 45
direction = 'right';
return
end
direction = 'front';
return
end
asbjos.m (my function, converted to MATLAB):
Code:
function direction = asbjos (angle)
% http://orbiter-forum.com/showthread.php?p=504866&postcount=11
name = {'front', 'right', 'back', 'left'};
direction = name{ floor(4 * mod(angle + 45, 360) / 360) + 1 }; % '+ 1' because MATLAB starts counting from 1, not 0
end