zendframework / zend-stdlib

Stdlib component from Zend Framework
BSD 3-Clause "New" or "Revised" License
384 stars 76 forks source link

Added the FastPriorityQueue implementation #19

Closed ezimuel closed 9 years ago

ezimuel commented 9 years ago

I added a new Stdlib's priority queue implementation based on my FastPriorityQueue project.

The class Zend\Stdlib\FastrPriorityQueue implements an efficient integer priority queue in pure PHP. This class acts like a queue, removing the elements using the extract() function, and it acts like an Iterator, without removing the elements using a loop, i.e. foreach.

The performance of this new class are very good, compared with Zend\Stdlib\SplPriorityQueue and Zend\Stdlib\PriorityQueue. I added a benchmark test in the /benchmark folder, using athletic.

Here the results of a test using an Intel Core i5-2500 at 3.30GHz with 8 Gb of RAM running Ubuntu Linux 14.04 and PHP 5.5.9:

ZendBench\Stdlib\PriorityQueue
    Method Name                Iterations    Average Time      Ops/second
    ------------------------  ------------  --------------    -------------
    insertSplPriorityQueue  : [5,000     ] [0.0000020447731] [489,051.81661]
    extractSplPriorityQueue : [5,000     ] [0.0000041113853] [243,227.01863]
    insertPriorityQueue     : [5,000     ] [0.0000032622814] [306,533.94723]
    extractPriorityQueue    : [5,000     ] [0.0000047439575] [210,794.46767]
    insertFastPriorityQueue : [5,000     ] [0.0000010054588] [994,570.80527]
    extractFastPriorityQueue: [5,000     ] [0.0000011510849] [868,745.65037]
akrabat commented 9 years ago

Does this have the same functionality as Zend\Stdlib\PriorityQueue?

weierophinney commented 9 years ago

@akrabat Yes; they both follow the same signature as SplPriorityQueue, and FastPriorityQueue acts like PriorityQueue in that it has predictable ordering of items with the same priority, and iteration does not remove items.

akrabat commented 9 years ago

Why not replace PriorityQueue with this one?

weierophinney commented 9 years ago

PriorityQueue implements IteratorAggregate vs Iterator, which means there's a slight API difference; additionally, it exposes a method for altering the internal queue class used. For BC purposes, it makes sense to keep both, though we can likely deprecate PriorityQueue when we release 2.6 with the new implementation.