By Matt Butcher
benchmarks
A Set of Objects in PHP: Arrays vs. SplObjectStorage
Submitted by matt on Fri, 2009-05-29 16:50One 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:
- Use good old fashioned arrays to emulate a hash set.
- 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.








Recent comments
28 min 18 sec ago
12 hours 47 min ago
17 hours 24 min ago
17 hours 46 min ago
18 hours 48 min ago
1 day 21 hours ago
2 days 11 hours ago
2 days 13 hours ago
2 days 15 hours ago
2 days 18 hours ago