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();
?> |
<?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 Read more »