Archive for the 'Web Development' Category Page 2 of 4



Preparing Gentoo Linux for a minimalist web server

This is a first stab at a guide to preparing a minimalist web server running on Gentoo Linux, ideal for a small VPS. Don’t follow these instructions unless you know what you’re doing, ’cause I’m not sure I do. :P (Apologies for the lack of updates; I’ve been busy playing with this Gentoo web server amongst other things.)

This is a draft. Comments, suggestions, corrections, improvements are very much welcome!

Continue reading ‘Preparing Gentoo Linux for a minimalist web server’

If you liked this post, please subscribe to my feed. Thanks for visiting!

PHP: speed of string replacement functions

What is the fastest string replacement function in PHP? sprintf() is probably the fastest, but it’s only useful in limited situations where the input is prepared for sprintf formatting. preg_replace() is obviously the slowest. The big debate is whether str_replace() or strtr() is faster.

The lixlpixel PHP benchmarks show that strtr is the faster of the two. I then came across Simple Machine’s benchmarks which show that str_replace() is faster. Hmm what’s going on?

Syridium DV’s PHP benchmarks performs more varied tests with these two functions. The relative speed of these two functions might vary depending on the type of string replacement you are performing. However, it seems that str_replace() is usually a safe bet.

Zenphoto plugin: Static Cache Control

Stealing a page from WP Super Cache’s book, I’ve written a plugin for Zenphoto that caches (and optionally gzips) Zenphoto pages on the file system to minimize PHP execution, making your dynamic image gallery run almost as fast and light on resources as a static website. Read more about Zenphoto staticCacheControl here.

Nagging question:
Using PHP, is it impossible to redirect a browser to a different URL without changing the browser’s URL visibly? In other words, can transparent URL rewriting Apache-style be accomplished with PHP?

Zenphoto plugin: Link Prefetching Hints

printPrefetchHints is a plugin that improves Zenphoto image browsing performance by printing link prefetching hints which instruct Mozilla-based browsers to preload the next page and image in the album.

Link prefetching hints are only supported by Mozilla-based browsers. The hints are completely harmless as they are ignored by other browsers like Internet Explorer. You can test your browser to see if it supports prefetching.

PHP: Measuring code execution time

The following code measures the amount of time it takes to execute a chunk of code.

In PHP 4 (source):

$time1 = explode(' ', microtime());
/* your chunk of code here */
$time2 = explode(' ', microtime());
$begintime = $time1[1] + $time1[0];
$endtime = $time2[1] + $time2[0];
$totaltime = $endtime - $begintime;
echo "\n<!-- Executed in $totaltime sec -->\n";

In PHP 5 (source):

$time_start = microtime(true);
/* your chunk of code here */
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "\n<!-- Executed in $time sec -->\n";

Zenphoto plugin: HTTP Cache Control

I’ve released a plugin for Zenphoto, httpCacheControl, which makes your Zenphoto pages cacheable in order to increase site performance and minimize resource usage. It uses the doConditionalGet() function I released yesterday.

Again, feedback, suggestions, improvements, criticisms very much welcome!

PHP: speed of string comparison functions

Here is a recent benchmark of various PHP string comparison functions. Not surprisingly, the fastest is strpos. But did you know that preg_match is faster than strstr and not much slower than strpos? This contradicts the following note in the official PHP manual:

Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.

ereg is by far the slowest.

So, the best function to use to do a simple check to see if a string contains a particular substring is strpos. In all other cases preg_match might be superior to strstr. YMMV.