Benchmarking!
If you have ever found yourself trying to make a script run faster, it can be a pain. GBench (a benchmarker) makes it less of a pain:
- You create a function you want to benchmark
- You join("utility_benchmark");
- You call this.defaultMain({this.bench("someName", temp.yourFunctionHere)});
It will run 100 samples (basically a sample is enough of your function calls to take up at least 0.01 secs).
Then it will give you a
histogram that shows each sample and how long it took.
And then it will give you something called a "
Kernel Density Estimation". That is a statistics term for a pretty simple concept:
In our case, look at any point on the
kernel density estimate, and that is your probability of your function taking that long.
Example!
PHP Code:
this.join("utility_benchmark");
//#CLIENTSIDE
// some example function you wrote
function fib(temp.n) {
if (temp.n == 0) {
return 0;
}
if (temp.n == 1) {
return 1;
}
return fib(temp.n-1) + fib(temp.n-2);
}
// just in case
function onCreated() {
this.maxlooplimit = 20000;
}
function onWeaponFired() {
// this is what we will be testing
temp.f = function () {
return this.fib(18);
};
// let's go!
this.defaultMain({
this.bench("fib", temp.f)
});
}
In your F2, you will get:
NPC Code:
benchmarking fib
collecting 100 samples, 1 iterations each, in estimated 4.057331309 s
mean: 41.656855829 ms, lb: 41.109135379 ms, ub: 42.937021324 ms
And, then you will get your
histogram and
kernel density estimate (all times are in milliseconds):
The big hill on the
kernel density estimate shows that most likely the function will take around 41.6 ms to run. The smaller hill near the end means there is a slight chance that the function will take 43 ms to run.
Known Bugs - On some computers, in v5, some samples take a negative amount of time to run. I have no clue why this would happen, since it just compares timevar2s.
- In v6, division doesn't work nicely and can make the graph axis a bit hard to read.
The magic is attached!