Come to the 2010 CMS Expo

benchmarks

A Set of Objects in PHP: Arrays vs. SplObjectStorage

One of my projects, QueryPath, performs many tasks that require maintaining a set of unique objects. In my quest to optimize QueryPath, I have been looking into various ways of efficiently storing sets of objects in a way that provides expedient containment checks. In other words, I want a data structure that keeps a list of unique objects, and can quickly tell me if some object is present in that list. The ability to loop through the contents of the list is also necessary.

Recently I narrowed the list of candidates down to two methods:

  1. Use good old fashioned arrays to emulate a hash set.
  2. Use the SPLObjectStorage system present in PHP 5.2 and up.

Before implementing anything directly in QueryPath, I first set out designing the two methods, and then ran some micro-benchmarks (with Crell's help) on the pair of methods. To say that the results were surprising is an understatement. The benchmarks will likely change the way I structure future code, both inside and outside of Drupal.

Syndicate content

Recent comments