Having discussed
what particular algorithms are better or worse than others, I made a benchmark function to, I hope, measure run-times. I'm not at all sure about the accuracy of this function, but I'd be interested in anyone else's input:
private ["_function", "_params", "_iterations", "_repeats", "_start"];
_function = _this select 0; // Function to test
_params = _this select 1; // Parameters to send to the function each time
_iterations = _this select 2; // Number of iterations to run without pausing.
_repeats = _this select 3; // Number of times to repeat the test.
_start = time;
for "_i" from 1 to _repeats do
{
for "_j" from 1 to _iterations do
{
_params call _function;
};
sleep 0.001;
};
(time - _start) / (_iterations * _repeats);
This should be more accurate a measurement, the larger the number of _
iterations and the higher the regular frame-rate (so run it on Rahmadi with low settings). If your game doesn't freeze completely for at least a couple of seconds, increase the number of iterations until it does, otherwise the graphics/game engine will be supplying a significant amount of time to that of your function. If the functions you want to test take a long time, reduce the number of iterations before you start, otherwise you could be there all day...
#define ITERATIONS 5000
#define REPEATS 3
private ["_benchmark", "_array", "_timeNOP", "_timePush", "_timeCat", "_timePush2", "_timeCat2"];
sleep 5; // Allow for system init.
// Baseline for doing nothing.
_array = [];
_timeNOP = [{ }, [], ITERATIONS, REPEATS] call _benchmark;
// Push one element onto array.
sleep 1;
_array = [];
_timePush = [{ _array set [count _array, 1] }, [], ITERATIONS, REPEATS] call _benchmark;
// Concatenate one element onto an array.
sleep 1;
_array = [];
_timeCat = [{ _array = _array + [1] }, [], ITERATIONS, REPEATS] call _benchmark;
hint format ["NOP: %1ms\nConcatenate: %2ms\nPush: %3ms",
_timeNOP * 1000,
(_timeCat - _timeNOP) * 1000,
(_timePush - _timeNOP) * 1000];
Incidentally, in my tests,
push one element (_a set [count _a,_b]) takes a lot less that
concatenate one value (_a = _a + [_b]) (using 2.5GHz quad core):
* 1000 iterations (x3) takes 1us and 6.7us per operation, respectively
* 5000 iterations (x3) takes 0.5us and 25.5us per operation, respectively
* 10000 iterations (x3) takes 0.4us and 17.6us per operation, respectively
Push is clearly much faster based on these tests, but I'm not sure if the ratio is at all accurate...
Anyone see problems with this benchmark system? I'm sure I may have ignored an important factor...
EDIT: Please don't discuss whether
push or concatenate is faster, or should be faster, here - I'm just giving that as an example to test benchmarking effectiveness.
EDIT2: Oops, marked timings as ns (nanoseconds) when I meant us (microseconds).