By Matt Butcher
benchmarks
A Set of Objects in PHP: Arrays vs. SplObjectStorage
Submitted by matt on Fri, 2009-05-29 15: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
2 days 6 hours ago
3 days 7 hours ago
3 days 16 hours ago
3 days 16 hours ago
4 days 23 hours ago
5 days 18 hours ago
6 days 19 hours ago
1 week 16 hours ago
1 week 3 days ago
2 weeks 2 days ago