Sha256: c1589691248822c5d8b2a66f54b7fe47bf22f235c68cbcb70445327fa42429f6

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

# PQ [![Build Status](https://travis-ci.org/zinenko/pq.svg?branch=master)](https://travis-ci.org/zinenko/pq)

Priority queue implementation [wiki](https://en.wikipedia.org/wiki/Priority_queue#Specialized_heaps)

## How to use:

```
queue = PQ.new

queue << 0
queue << 1
queue << -1
queue << +100500

queue.each do |i|
  puts i
end

# => 100500
# => 1
# => 0
# => -1
```

## Performance

```
require 'pq'
require 'benchmark/ips'

class NaiveQueue
  def initialize
    @elements = []
  end

  def <<(element)
    @elements << element
  end

  def pop
    last_element_index = @elements.size - 1
    @elements.sort!
    @elements.delete_at(last_element_index)
  end
end

naive_queue = NaiveQueue.new
priority_queue = PQ.new

100_000.times do |i|
  naive_queue << i
  priority_queue  << i
end

Benchmark.ips do |x|
  x.report("naive") { naive_queue.pop }
  x.report("pq")    { priority_queue.pop  }

  x.compare!
end
```

## Result

```
Warming up --------------------------------------
               naive   178.000  i/100ms
                  pq    20.738k i/100ms
Calculating -------------------------------------
               naive      1.888k (± 6.1%) i/s -      9.434k in   5.016797s
                  pq      1.243M (± 9.9%) i/s -      6.138M in   5.001097s

Comparison:
                  pq:  1242931.0 i/s
               naive:     1888.0 i/s - 658.33x  slower
```

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/pq-0.0.1/README.md