Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Simplify SplPriorityQueue::serialize()
Browse files Browse the repository at this point in the history
$queue = new \Zend\Stdlib\SplPriorityQueue;
for ($i=0; $i<100; $i++) {
    $queue->insert('hello'.$i, $i);
}

// Before
$time = microtime(true);
for ($i=0; $i<1000; $i++) {
    $serialized = $queue->serialize();
}
echo (microtime(true) - $time); // 0.75588607788086

// After
$time = microtime(true);
for ($i=0; $i<1000; $i++) {
    $serialized = $queue->serialize();
}
echo (microtime(true) - $time); // 0.28470015525818
  • Loading branch information
EvanDotPro committed Jul 30, 2012
1 parent 982c8a4 commit d6a00a6
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/SplPriorityQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,20 @@ public function toArray()
return $array;
}


/**
* Serialize
*
* @return string
*/
public function serialize()
{
$data = array();
$this->setExtractFlags(self::EXTR_BOTH);
while ($this->valid()) {
$data[] = $this->current();
$this->next();
}
$this->setExtractFlags(self::EXTR_DATA);
$clone = clone $this;
$clone->setExtractFlags(self::EXTR_BOTH);

// Iterating through a priority queue removes items
foreach ($data as $item) {
$this->insert($item['data'], $item['priority']);
$data = array();
foreach ($clone as $item) {
$data[] = $item;
}

return serialize($data);
Expand Down

0 comments on commit d6a00a6

Please sign in to comment.