Monthly Archives: October 2010

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 Read more »

Starting up my CSS Minify project

This is just a status update on my CSS Minify project, I will make more updates as I progress through the project. It’s basically a journal of my thoughts and design process, so if anything strikes you please do comment with some feedback.

So I started working on a way to minify my CSS files. I know (or well I found out, just after I started working on my own project) that there’s a well written and very functional CSSTidy which does exactly what I want to do. I will anyhow still continue my own project, mostly for the learning process, but also because it’s been a long time since I’ve had a project of my own and I miss that.
My initial thought was to parse the CSS using regular expressions, but the lexical approach CSSTidy uses caught my eye.
I’ve decided to start out parsing with the regular expressions. I will eventually try to copycat the CSSTidy way, just to get a better understanding of lexical analysis, and having CSSTidy as a result sheet.

So far I’ve made the parser, and it seems to work on proper CSS I will need to test it up against some messy CSS.
I had my CSSParser class, with a $selectors array holding all the selectors (my CSSSelector classes). The CSSSelector class had a similar $properties array, holding all the properties (CSSProperties class) for the selector.
This seemed a bit overkill, the arrays were both using a key being the selector/property name and a value being the class, the class it self just being a class with a value property. So I will remove the 2(selector and property) classes, and just use a simpler array structure.
I do however still like the idea of having the selector and property classes, it fits nicely into the whole CSS principal. I’ve been planing to use them when it comes to outputting the CSS again, but in that case why not just use them to start with … I’m torn.