Tag Archives: bench-class

Checking for a value

Reasoning behind the bench

I’ve been working a bit on my CSSParser (which I will rename to CSSReader og CSSHandler when I get around to it), and one thing I found out was, when reading shorthand values (like: border: 1px solid #00ffff;) you can’t assume they always come in the same order.
If we take a border property as an example:

.something {
  border-right: 1px dashed #00ff00;
  border-left: dashed 1px rgb(255,0,0);
  border-top blue solid 1px;

This shows that the border shorthand value can have a different order of subvalues, and the actual value data can change alot (notice the 3 different ways of defining a color, the rgb() still cause me a bit of trouble). So to set an array (mapped with [‘width’], [‘style’] and [‘color’]) I can’t just split the string and set it, I need to split the string and then try to guess every value. To guess the values I’ll be running through the following process: Read more »

Benchmark class, release (& first test)

So as promised I’ve cleaned up and commented a bit on my Bench class. And let me get to it, you can

Download it here.

Bench suite

I’ve also made my first test in my new (and not to pretty, but working) bench suite. The bench suite is a site where I can gather my different tests, and be able to easily access them later on for references. So far it’s a very simple setup, where I have:

  • A list of the tests. This goes through a directory where each test has it’s own test file, and use some of the data in the file to make an informative list.
  • A runner. Which will run each test, and output the results in an easy to read table, along with the actual source code of the test.

Down the road I would like to improve a bit on the interface which is lacking to say the least. I’ve also been planning on saving the test data so I can get an average result of all the tests run.

Empty loops test

So the very first test I’ve done is a simple empty loop(it will run the test every time you, so the result wont be identical to what I’ve displayed below) test. And it also works as a quick how-to for the class it self.
What I wanted to test with this bench, was the difference between the for, while and do..while loops, and as an added bonus I also tested a goto loop (more for the fun of things). I’ve also tested everything with a pre and post increment. Anyway the result of the test looks as following Read more »

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:

function recursive($c) {
    if ($c == 0)
    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");
$b->end("recursive", "1000");
$b->start("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 »

Benchmark class, what do I need?

Although micro-optimizing don’t give the biggest performance improvement, I often find my self wondering if using function X() instead of Y() is faster, and if my own function Z() is better. Most of the time I end up writing a small tiny benchmark test on the two or three functions, which then gives me a quick overview of the functions speed.
So I’ve decided to make a small and simple to use micro-benchmarking class.


Basically this class is all about being easy to use, so you don’t have to spend a long time setting up a test.

  • Easy to set up. The class should be used for those small tests, where you’re in doubt of which piece of code is fastest. And if one is to test a piece of code, for a tiny performance increase (maybe more out of curiosity than an actual application improvement), it should only take a couple of minutes to set the test up. If it takes 15 minutes to create a test every time, you wont take the time to write the test if you’re in the middle of something else.
  • Readable output. For the same reasons it should be easy to set up, the output needs to be readable when the test is done, without any post-test-processing. It would be good if the output could be chosen, so you could either get the easy to read, or a more in-depth detailed result (maybe formatted for post-processing in another environment).

Nice to have

Again with the intentions of making this easier to work with, it would be great to have a feature, where you would just write the piece of code you wanted to test, and then have the class handle the rest. Read more »