Archive for the ‘Web’ Category

PHP Array Performance

Friday, February 26th, 2010

Index Checking

The functional difference between array_key_exists($key, $array) and isset($array[$key]) is that isset returns false when the key exists but contains null. Functionality aside, there is a demonstrable performance difference.

$dataSet = array();
$size = 0x1FFFF;

$i = $size;
while ($i--)
	$dataSet[$i] = $i+1;

$start = microtime(true);

$i = $size;
while ($i--)
	array_key_exists($i, $dataSet); //isset($dataSet[$i]);

echo "Time: ". (microtime(true) - $start);

Sure enough, after averaging a few benchmarks together the outcome was clear.

isset
0.0248870849609
array_key_exists
0.604112148285

When performance is important target isset() when applicable. It’s even possible to use a fall through to increase performance, here’s an example from my Options class: if (!isset($this->_options[$resolvedName]) && !array_key_exists($resolvedName, $this->_options))

Append Performance

In the following test case there was no performance difference between usage of append or directly addressing an index.

$dataSet = array();
$size = 0x1FFFF;
$i = $size;

$start = microtime(true);

while ($i--)
	$dataSet[] = $i+1; //$dataSet[$i] = $i+1;

echo "Time: ". (microtime(true) - $start);

Remodeling PHP’s compact()/extract() into an OO Pattern

Sunday, October 11th, 2009

A common usage, at least for extract(), has been to allow an associative array to be passed to a function for use as named optional parameters, however, there is a lot wrong with dumping an array into the symbol table and then checking if what you want exists or not.

I have designed a pattern that allows an object to accept and address public level optional named arguments without the problems that could be associated with the compact()/extract() pattern. This pattern does require at least PHP 5.3 (uses late static binding) and overloads __get/__set preventing the child class from doing so.

(more…)

Queryable Food Nutrition Database

Wednesday, October 7th, 2009

The USDA releases data for food nutritional information. I’ve taken this data and loaded it into my local database, and created a front end to allow it to be queryable.

Database Schema

database schema

(more…)

Message Hasher

Monday, October 5th, 2009

A small web-based message hasher, good for generating hashes of small amounts of text. No binary support yet, but it’s planned via file uploads!

(more…)

Creating a Google Page Rank Finder in Javascript

Wednesday, July 22nd, 2009

An interesting technology are Google Page Rank Finders. What they do is discover which result and page number a website is in Google’s search results for a given term.

I thought an interesting implementation would be a completely client side one written in Javascript using Google’s search API. To spoil the ending, after creating this I discovered the API only allows 8 results per page and 8 pages, making this useless if the site is beyond the 64th result, but the API is interesting and powerful regardless.

The finished version can be found at pagerank.red-stars.net with the complete Javascript.

(more…)