<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>Geert De Deckere</title>
	
	<link>http://www.geertdedeckere.be</link>
	<description>Web developer</description>
	<lastBuildDate>Sun, 19 Apr 2009 13:58:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/geertdedeckere" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="geertdedeckere" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Saving a HTTP request by combining screen and print styles</title>
		<link>http://www.geertdedeckere.be/article/saving-a-http-request-by-combining-screen-and-print-styles</link>
		<comments>http://www.geertdedeckere.be/article/saving-a-http-request-by-combining-screen-and-print-styles#comments</comments>
		<pubDate>Sun, 19 Apr 2009 13:58:30 +0000</pubDate>
		<dc:creator>Geert De Deckere</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[Optimization]]></category>

		<guid isPermaLink="false">http://www.geertdedeckere.be/?p=35</guid>
		<description><![CDATA[<p>Use @media rules to put screen and print styles in the same file and speed up your website while doing so.</p>]]></description>
			<content:encoded><![CDATA[<p>Minimizing HTTP requests is a very effective way for speeding up your website. In his book “<a href="http://oreilly.com/catalog/9780596529307/">High Performance Web Sites</a>”, Steve Souders talks about this practice as the number one rule to front-end optimization.</p>

<p>Some well-known techniques to reduce HTTP requests include using <a href="http://alistapart.com/articles/sprites">CSS sprites</a> and combining multiple scripts and stylesheets into single files. Ideally you should be using only one stylesheet. However, how many times have you seen the following two lines in the <code>&lt;head&gt;</code> of a HTML page?</p>


<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen.css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;print.css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;print&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>





<h2>Combining screen and print styles</h2>

<p>I get the impression many people are unaware of the fact you can combine screen and print styles into a single stylesheet. Actually you can combine all media types, and it is easy to do.</p>

<h3>Linking to your stylesheet</h3>


<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;styles.css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen, print&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>




<p>You may be wondering why I did not use <code>media="all"</code> or simply omitted the <code>media</code> attribute. By stating the media types the stylesheet applies to right there in the <code>&lt;link&gt;</code>, user agents may decide not to load the stylesheet at all if it does not apply to the current device. Another possible HTTP request less.</p>

<h3>Splitting up your stylesheet</h3>

<p>All that is left to do is telling the browser which parts of <code>styles.css</code> apply to screen and which parts apply to print. The example below is self-explanatory.</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #a1a100;">@media screen {</span>
	body <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">14px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #a1a100;">@media print {</span>
	body <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">10pt</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
	h1<span style="color: #00AA00;">,</span> h2<span style="color: #00AA00;">,</span> h3 <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">page-break-after</span><span style="color: #00AA00;">:</span><span style="color: #993333;">avoid</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>




<p>Styles that apply to both screen and print do not need not be wrapped in an <code>@media</code> rule in this case since the stylesheet itself applies to both media types already, as specified in the <code>&lt;link&gt;</code>. Just for the sake of completeness, though, note that you can specify multiple media types separated by commas.</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #a1a100;">@media screen, print {</span>
	body <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">1.4</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>




<p>To freshen up your knowledge about <code>@media</code> rules, I recommend you have a look at the CSS specification for <a href="http://www.w3.org/TR/CSS21/media.html">media types</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.geertdedeckere.be/article/saving-a-http-request-by-combining-screen-and-print-styles/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Introducing Codebench</title>
		<link>http://www.geertdedeckere.be/article/introducing-codebench</link>
		<comments>http://www.geertdedeckere.be/article/introducing-codebench#comments</comments>
		<pubDate>Sat, 18 Apr 2009 15:30:52 +0000</pubDate>
		<dc:creator>Geert De Deckere</dc:creator>
				<category><![CDATA[Kohana]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[Codebench]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[PCRE]]></category>

		<guid isPermaLink="false">http://www.geertdedeckere.be/?p=11</guid>
		<description><![CDATA[<p>Codebench is a Kohana module for benchmarking PHP code. It comes in really handy when tweaking regular expressions.</p>]]></description>
			<content:encoded><![CDATA[<p>For a long time I have been using a quick-and-dirty <code>benchmark.php</code> file to optimize bits of PHP code, many times regex-related stuff. The file contained not much more than a <a href="http://php.net/gettimeofday"><code>gettimeofday</code></a> function wrapped around a <code>for</code> loop. It worked, albeit not very efficiently. Something more solid was needed. I set out to create a far more usable piece of software to aid in the everlasting quest to squeeze every millisecond out of those regular expressions.</p>


<h2>Codebench goals</h2>

<h3>Benchmark multiple regular expressions at once</h3>

<p>Being able to compare the speed of an arbitrary amount of regular expressions would be tremendously useful. In case you are wondering—yes, I had been writing down benchmark times for each regex, uncommenting them one by one. You get the idea. Those days should be gone forever now.</p>

<h3>Benchmark multiple subjects at once</h3>

<p>What gets overlooked too often when testing and optimizing regular expressions is the fact that speed can vastly differ depending on the subjects, also known as input or target strings. Just because your regular expression matches, say, a valid email address quickly, does not necessarily mean it will quickly realize when an <em>invalid</em> email is provided. I plan to write a follow-up article with hands-on regex examples to demonstrate this point. Anyway, Codebench allows you to create an array of subjects which will be passed to each benchmark.</p>

<h3>Make it flexible enough to work for all PCRE functions</h3>

<p>Initially I named the module “Regexbench”. I quickly realized, though, it would be flexible enough to benchmark all kinds of PHP code, hence the change to “Codebench”. While tools specifically built to help profiling PCRE functions, like <a href="http://php.net/preg_match"><code>preg_match</code></a> or <a href="http://php.net/preg_replace"><code>preg_replace</code></a>, definitely have their use, more flexibility was needed here. You should be able to compare all kinds of constructions like combinations of PCRE functions and native PHP string functions.</p>

<h3>Create clean and portable benchmark cases</h3>

<p>Throwing valuable benchmark data away every time I needed to optimize another regular expression had to stop. A clean file containing the complete set of all regex variations to compare, together with the set of subjects to test them against, would be more than welcome. Moreover, it would be easy to exchange benchmark cases with others.</p>

<h3>Visualize the benchmarks</h3>

<p>Obviously providing a visual representation of the benchmark results, via simple graphs, would make interpreting them easier. Having not to think about Internet Explorer for once, made writing CSS a whole lot more easy and fun. It resulted in some fine graphs which are fully resizable.</p>

<p>Below are two screenshots of Codebench in action. <code>Valid_Color</code> is a class made for benchmarking different ways to validate hexadecimal HTML color values, e.g. <code>#FFF</code>. If you are interested in the story behind the actual regular expressions, take a look at <a href="http://forum.kohanaphp.com/comments.php?DiscussionID=2192">this topic in the Kohana forums</a>.</p>

<p class="figure x-large">
<a class="left large" href="http://www.geertdedeckere.be/wp-content/uploads/2009/04/codebench_screenshot1.png"><img src="http://www.geertdedeckere.be/wp-content/uploads/2009/04/codebench_screenshot1-480x249.png" alt="Codebench screenshot" width="480" height="249" /></a>
<span class="pull small">Benchmarking seven ways to validate HTML color values</span>
</p>

<p class="figure x-large">
<a class="left large" href="http://www.geertdedeckere.be/wp-content/uploads/2009/04/codebench_screenshot2.png"><img src="http://www.geertdedeckere.be/wp-content/uploads/2009/04/codebench_screenshot2-480x182.png" alt="Codebench screenshot" width="480" height="182" /></a>
<span class="pull small">Collapsable results per subject for each method</span>
</p>


<h2>Working with Codebench</h2>

<p><strong><a href="http://github.com/GeertDD/codebench/tree/master">Download Codebench</a></strong> at GitHub. Since Codebench is a module for the Kohana PHP framework, I suggest you install <a href="http://kohanaphp.com/">Kohana</a> first if you have not already done so. Add Codebench to the list of activated modules in <code>application/config.php</code>.</p>

<p>Creating your own benchmarks is just a matter of creating a library that extends the Codebench class. Put the code parts you want to compare into separate methods. Be sure to prefix those methods with “bench”, other methods will not be benchmarked. Glance at <code>Valid_Color.php</code> in the download for a real example.</p>

<p>Here is another short example with some extra explanations.</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// libraries/Ltrim_Digits.php</span>
<span style="color: #000000; font-weight: bold;">class</span> Ltrim_Digits <span style="color: #000000; font-weight: bold;">extends</span> Codebench <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Some optional explanatory comments about the benchmark file.</span>
    <span style="color: #666666; font-style: italic;">// HTML allowed. URLs will be converted to links automatically.</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$description</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Trimming leading digits: regex vs ltrim.'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// How many times to execute each method per subject.</span>
    <span style="color: #666666; font-style: italic;">// Total loops = loops * number of methods * number of subjects</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$loops</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100000</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// The subjects to supply iteratively to your benchmark methods.</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$subjects</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span>
    <span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'123digits'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'no-digits'</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> bench_regex<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^\d+/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> bench_ltrim<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">ltrim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'0..9'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>And the winner is… <a href="http://php.net/ltrim"><code>ltrim</code></a>. Happy benchmarking!</p>]]></content:encoded>
			<wfw:commentRss>http://www.geertdedeckere.be/article/introducing-codebench/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

