Benchmark class v1

So I’ve made a first version of the benchmarking class I’ve been talking about previously. While working on the CSSParser I encountered one of those micro-optimization situations, so I made a quick first draft of the Bench class (I’ll post the actual benchmark in another post).
The outline of the Bench class is as the following:

Function Description
start($test, $subTest = “”) Start a test, with a test title (which is also the identifier for the test). It’s possible to supply additional sub tests to the test, this is helpful when doing the same code test but with different inputs.
end($test, $subTest = “”) Ends the test with the test and subTest identifier.
tick($test, $subTest = “”) Set a mid point in a test, this way we can easily make more fix points if needed.
getResult($dec = 6) Returns all the tests in a neatly packed table, the $dec is the amount of decimals on the time measurement.

Let me show a simple example where we test 2 empty for and while loops and a recursive function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
function recursive($c) {
    if ($c == 0)
        return;
    return recursive(--$c);
}
 
$b = new Bench();
 
$b->start("for loop", "1000");
for ($i = 0; $i < 1000; $i++) {
}
$b->end("for loop", "1000");
$b->start("for loop", "10000");
for ($i = 0; $i < 10000; $i++) {
}
$b->end("for loop", "10000");
 
$b->start("while", "1000");
$i = 0;
while ($i++ < 1000) {
}
$b->end("while", "1000");
$b->start("while", "10000");
$i = 0;
while ($i++ < 10000) {
}
$b->end("while", "10000");
 
$b->start("recursive", "1000");
recursive(1000);
$b->end("recursive", "1000");
$b->start("recursive", "10000");
recursive(10000);
$b->end("recursive", "10000");
 
echo $b->getResult();
?>
simple bench output

for loop 	0.000097	0.000810	0.000454	100.00%
while    	0.000074	0.000629	0.000351	77.48%
recursive	0.001844	0.016954	0.009399	2072.14%

The first column is the name of the test.
The following 2 columns is each sub test, so the first number is the time of our “1000″ iterations and the second number is “10000″ iterations.
The fourth column represents the average of all the sub tests (it’s not always this is informative, so there will be an option to remove this in future versions).
The last column is the difference between all the tests average. It will set the first test to 100%, the rest of the tests will be calculated in relation to this number.
With the output formatted this way, you can easily copy and paste it into a spreadsheet and display it in what ever chart that fits your purpose. Just remember not to use the percentage in your data set:
An example of the data in a Google Docs spreadsheet

I still need to clean up and comment the class some more, before I’m comfortable with putting it up online. But it’s rather small so if I get some time this weekend I’ll run through it, and it will be up next week.

Leave a comment

0 Comments.

Leave a Reply

[ Ctrl + Enter ]