d_heap 0.2.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -1,14 +1,20 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require "rspec/core/rake_task"
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
6
12
  require "rake/extensiontask"
7
13
 
8
- task :build => :compile
14
+ task build: :compile
9
15
 
10
16
  Rake::ExtensionTask.new("d_heap") do |ext|
11
17
  ext.lib_dir = "lib/d_heap"
12
18
  end
13
19
 
14
- task :default => [:clobber, :compile, :spec]
20
+ task default: %i[clobber compile spec rubocop]
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "perf"
5
+
6
+ system("#{RbConfig.ruby} bin/rake compile", err: :out, exception: true)
7
+ require "d_heap/benchmarks"
8
+ include DHeap::Benchmarks # rubocop:disable Style/MixinUsage
9
+ fill_random_vals
10
+
11
+ n = ENV.fetch("BENCH_N", 5_000_000).to_i
12
+ # interval = ENV.fetch("PROF_INTERVAL", 100).to_i # measured in μs
13
+
14
+ i = 0
15
+
16
+ q = initq RbHeap
17
+ n.times { q << n }
18
+ q.clear
19
+
20
+ Perf.record do
21
+ while i < n
22
+ q << random_val
23
+ i += 1
24
+ end
25
+ while 0 < i # rubocop:disable Style/NumericPredicate
26
+ q.pop
27
+ i -= 1
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ ---
2
+ prelude: |
3
+ system("#{RbConfig.ruby} bin/rake compile", err: :out, exception: true)
4
+ require "d_heap/benchmarks"
5
+ include DHeap::Benchmarks
6
+ fill_random_vals
7
+
8
+ N = ENV.fetch("BENCH_N", 1000).to_i
9
+
10
+ benchmark:
11
+ - script: &script |
12
+ if __bmdv_i % N == 0
13
+ q.clear
14
+ end
15
+
16
+ q << random_val
17
+ name: "push N (findmin)"
18
+ prelude: "q = initq FindMin"
19
+ loop_count: 24000000
20
+ - script: *script
21
+ name: "push N (bsearch)"
22
+ prelude: "q = initq BSearch"
23
+ loop_count: 2300000
24
+ - script: *script
25
+ name: "push N (rb_heap)"
26
+ prelude: "q = initq RbHeap"
27
+ loop_count: 9800000
28
+ - script: *script
29
+ name: "push N (c++ stl)"
30
+ prelude: "q = initq CppSTL"
31
+ loop_count: 18700000
32
+ - script: *script
33
+ name: "push N (c_dheap)"
34
+ prelude: "q = initq DHeap"
35
+ loop_count: 25100000
@@ -0,0 +1,52 @@
1
+ ---
2
+ prelude: |
3
+ system("#{RbConfig.ruby} bin/rake compile", err: :out, exception: true)
4
+ require "d_heap/benchmarks"
5
+ include DHeap::Benchmarks
6
+ fill_random_vals
7
+
8
+ N = ENV.fetch("BENCH_N", 1000).to_i
9
+ N2 = N * 2
10
+
11
+ i = j = 0
12
+
13
+ benchmark:
14
+ - script: &script |
15
+ if i < N
16
+ q.clear if __bmdv_i == 0
17
+ q << random_val
18
+ i += 1
19
+
20
+ elsif j < N
21
+ q.pop
22
+ j += 1
23
+
24
+ elsif q.empty?
25
+ i = 1
26
+ j = 0
27
+ q.clear
28
+ q << random_val
29
+
30
+ else
31
+ raise "q not empty!"
32
+ end
33
+
34
+ name: "push N + pop N (findmin)"
35
+ prelude: "q = initq FindMin"
36
+ loop_count: 200000
37
+ - script: *script
38
+ name: "push N + pop N (bsearch)"
39
+ prelude: "q = initq BSearch"
40
+ loop_count: 4000000
41
+ - script: *script
42
+ name: "push N + pop N (rb_heap)"
43
+ prelude: "q = initq RbHeap"
44
+ loop_count: 4000000
45
+ - script: *script
46
+ name: "push N + pop N (c++ stl)"
47
+ prelude: "q = initq CppSTL"
48
+ loop_count: 16000000
49
+ - script: *script
50
+ name: "push N + pop N (c_dheap)"
51
+ prelude: "q = initq DHeap"
52
+ loop_count: 16000000
@@ -0,0 +1,32 @@
1
+ ---
2
+ prelude: |
3
+ system("#{RbConfig.ruby} bin/rake compile", err: :out, exception: true)
4
+ require "d_heap/benchmarks"
5
+ include DHeap::Benchmarks
6
+ fill_random_vals
7
+
8
+ n = ENV.fetch("BENCH_N", 1000).to_i
9
+
10
+ benchmark:
11
+ - script: &script |
12
+ q << random_val
13
+ q.pop
14
+ name: "push + pop (findmin)"
15
+ prelude: "q = FindMin.new(n) { random_val }"
16
+ loop_count: 250000
17
+ - script: *script
18
+ name: "push + pop (bsearch)"
19
+ prelude: "q = BSearch.new(n) { random_val }"
20
+ loop_count: 5000000
21
+ - script: *script
22
+ name: "push + pop (rb_heap)"
23
+ prelude: "q = RbHeap.new(n) { random_val }"
24
+ loop_count: 2000000
25
+ - script: *script
26
+ name: "push + pop (c++ stl)"
27
+ prelude: "q = initq CppSTL, n"
28
+ loop_count: 13000000
29
+ - script: *script
30
+ name: "push + pop (c_dheap)"
31
+ prelude: "q = initq DHeap, n"
32
+ loop_count: 20000000
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "stackprof"
5
+
6
+ system("#{RbConfig.ruby} bin/rake compile", err: :out, exception: true)
7
+ require "d_heap/benchmarks"
8
+ include DHeap::Benchmarks # rubocop:disable Style/MixinUsage
9
+ fill_random_vals
10
+
11
+ n = ENV.fetch("BENCH_N", 5_000_000).to_i
12
+ interval = ENV.fetch("PROF_INTERVAL", 100).to_i # measured in μs
13
+
14
+ i = 0
15
+
16
+ q = initq RbHeap
17
+ n.times { q << n }
18
+ q.clear
19
+
20
+ StackProf.run(mode: :cpu, out: "stackprof-cpu-push.dump", interval: interval) do
21
+ while i < n
22
+ q << random_val
23
+ i += 1
24
+ end
25
+ end
26
+ StackProf.run(mode: :cpu, out: "stackprof-cpu-pop.dump", interval: interval) do
27
+ while 0 < i # rubocop:disable Style/NumericPredicate
28
+ q.pop
29
+ i -= 1
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ #!/bin/bash
2
+ set -eu
3
+ SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null; pwd -P)
4
+ PROJECT_DIR=$(cd "$SCRIPT_DIR" > /dev/null; cd .. > /dev/null; pwd -P)
5
+ cd "$PROJECT_DIR"
6
+
7
+ opts=( --bundle -e 'N 100000;N 10000;N 1000;N 100;N 10' -o gruff )
8
+
9
+ for bm in push_n push_n_pop_n push_pop; do
10
+ bin/benchmark-driver "${opts[@]}" "benchmarks/${bm}.yml"
11
+ mv graph.png "images/${bm}.png"
12
+ done
13
+
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+ set -eu
3
+
4
+ export BENCH_N="$1"
5
+ shift
6
+
7
+ exec ruby "$@"
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'benchmark-driver' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("benchmark_driver", "benchmark-driver")
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "pathname"
5
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
6
+ require "rubygems"
7
+ require "bundler/setup"
8
+
9
+ require "d_heap/benchmarks/benchmarker"
10
+ DHeap::Benchmarks::Benchmarker.new.call
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require "bundler/setup"
4
5
  require "d_heap"
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "pathname"
5
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
6
+ require "rubygems"
7
+ require "bundler/setup"
8
+
9
+ require "d_heap/benchmarks/profiler"
10
+ DHeap::Benchmarks::Profiler.new.call
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rubocop", "rubocop")
@@ -1,4 +1,6 @@
1
- require_relative 'lib/d_heap/version'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/d_heap/version"
2
4
 
3
5
  Gem::Specification.new do |spec|
4
6
  spec.name = "d_heap"
@@ -6,14 +8,14 @@ Gem::Specification.new do |spec|
6
8
  spec.authors = ["nicholas a. evans"]
7
9
  spec.email = ["nicholas.evans@gmail.com"]
8
10
 
9
- spec.summary = %q{A d-ary heap implementation, for priority queues}
11
+ spec.summary = "A d-ary heap implementation, for priority queues"
10
12
  spec.description = <<~DESC
11
13
  A C extension implementation of a d-ary heap data structure, suitable for
12
14
  use in e.g. priority queues or Djikstra's algorithm.
13
15
  DESC
14
16
  spec.homepage = "https://github.com/nevans/#{spec.name}"
15
17
  spec.license = "MIT"
16
- spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
18
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
17
19
 
18
20
  spec.metadata["homepage_uri"] = spec.homepage
19
21
  spec.metadata["source_code_uri"] = spec.homepage
@@ -21,11 +23,14 @@ Gem::Specification.new do |spec|
21
23
 
22
24
  # Specify which files should be added to the gem when it is released.
23
25
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
- end
26
+ spec.files = Dir.chdir(File.expand_path(__dir__)) {
27
+ `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/}) }
28
+ }
27
29
  spec.bindir = "exe"
28
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
29
31
  spec.require_paths = ["lib"]
30
32
  spec.extensions = ["ext/d_heap/extconf.rb"]
33
+
34
+ spec.add_development_dependency "benchmark_driver"
35
+ spec.add_development_dependency "ruby-prof"
31
36
  end
@@ -0,0 +1,75 @@
1
+ Warming up --------------------------------------
2
+ push + pop (findmin) 136.044 i/s - 140.000 times in 1.029078s (7.35ms/i)
3
+ push + pop (bsearch) 5.700k i/s - 5.984k times in 1.049902s (175.45μs/i)
4
+ push + pop (rb_heap) 466.447k i/s - 496.977k times in 1.065453s (2.14μs/i)
5
+ push + pop (c++ stl) 3.023M i/s - 3.038M times in 1.004830s (330.81ns/i, 794clocks/i)
6
+ push + pop (c_dheap) 3.579M i/s - 3.620M times in 1.011429s (279.40ns/i, 785clocks/i)
7
+ Calculating -------------------------------------
8
+ bin/bench_n 1000000 bin/bench_n 10000 bin/bench_n 25000 bin/bench_n 50000 bin/bench_n 100000 bin/bench_n 250000 bin/bench_n 500000 bin/bench_n 10000000 bin/bench_n 2500000 bin/bench_n 5000000
9
+ push + pop (findmin) 136.690 11.014k 4.426k 2.178k 1.084k 408.671 215.680 13.169 52.037 26.307 i/s - 408.000 times in 2.984861s 0.037045s 0.092186s 0.187334s 0.376306s 0.998359s 1.891687s 30.981982s 7.840594s 15.509132s
10
+ push + pop (bsearch) 5.639k 786.334k 364.964k 200.521k 88.607k 34.530k 17.965k 288.811 1.302k 592.009 i/s - 17.098k times in 3.032244s 0.021744s 0.046848s 0.085268s 0.192965s 0.495157s 0.951721s 59.201408s 13.131805s 28.881310s
11
+ push + pop (rb_heap) 513.523k 736.618k 670.187k 618.157k 579.251k 572.795k 543.648k 423.119k 460.849k 445.235k i/s - 1.399M times in 2.724978s 1.899681s 2.087985s 2.263730s 2.415776s 2.443003s 2.573980s 3.307202s 3.036440s 3.142928s
12
+ push + pop (c++ stl) 3.396M 4.902M 4.794M 4.532M 4.316M 4.204M 3.903M 2.022M 2.659M 2.347M i/s - 9.069M times in 2.670712s 1.850114s 1.891786s 2.001185s 2.101354s 2.157093s 2.323676s 4.484351s 3.410224s 3.864573s
13
+ push + pop (c_dheap) 4.403M 7.311M 6.407M 6.738M 6.254M 5.918M 5.126M 2.138M 3.304M 2.665M i/s - 10.737M times in 2.438365s 1.468580s 1.675785s 1.593589s 1.716764s 1.814447s 2.094553s 5.022305s 3.249709s 4.029170s
14
+
15
+ Comparison:
16
+ push + pop (findmin)
17
+ bin/bench_n 10000: 11013.7 i/s
18
+ bin/bench_n 25000: 4425.8 i/s - 2.49x slower
19
+ bin/bench_n 50000: 2177.9 i/s - 5.06x slower
20
+ bin/bench_n 100000: 1084.2 i/s - 10.16x slower
21
+ bin/bench_n 250000: 408.7 i/s - 26.95x slower
22
+ bin/bench_n 500000: 215.7 i/s - 51.06x slower
23
+ bin/bench_n 1000000: 136.7 i/s - 80.57x slower
24
+ bin/bench_n 2500000: 52.0 i/s - 211.65x slower
25
+ bin/bench_n 5000000: 26.3 i/s - 418.66x slower
26
+ bin/bench_n 10000000: 13.2 i/s - 836.34x slower
27
+
28
+ push + pop (bsearch)
29
+ bin/bench_n 10000: 786334.2 i/s
30
+ bin/bench_n 25000: 364963.8 i/s - 2.15x slower
31
+ bin/bench_n 50000: 200520.6 i/s - 3.92x slower
32
+ bin/bench_n 100000: 88607.0 i/s - 8.87x slower
33
+ bin/bench_n 250000: 34530.5 i/s - 22.77x slower
34
+ bin/bench_n 500000: 17965.4 i/s - 43.77x slower
35
+ bin/bench_n 1000000: 5638.7 i/s - 139.45x slower
36
+ bin/bench_n 2500000: 1302.0 i/s - 603.93x slower
37
+ bin/bench_n 5000000: 592.0 i/s - 1328.25x slower
38
+ bin/bench_n 10000000: 288.8 i/s - 2722.66x slower
39
+
40
+ push + pop (rb_heap)
41
+ bin/bench_n 10000: 736618.2 i/s
42
+ bin/bench_n 25000: 670186.8 i/s - 1.10x slower
43
+ bin/bench_n 50000: 618156.7 i/s - 1.19x slower
44
+ bin/bench_n 100000: 579250.7 i/s - 1.27x slower
45
+ bin/bench_n 250000: 572795.0 i/s - 1.29x slower
46
+ bin/bench_n 500000: 543648.3 i/s - 1.35x slower
47
+ bin/bench_n 1000000: 513523.4 i/s - 1.43x slower
48
+ bin/bench_n 2500000: 460848.9 i/s - 1.60x slower
49
+ bin/bench_n 5000000: 445234.5 i/s - 1.65x slower
50
+ bin/bench_n 10000000: 423119.0 i/s - 1.74x slower
51
+
52
+ push + pop (c++ stl)
53
+ bin/bench_n 10000: 4901711.5 i/s
54
+ bin/bench_n 25000: 4793735.8 i/s - 1.02x slower
55
+ bin/bench_n 50000: 4531675.9 i/s - 1.08x slower
56
+ bin/bench_n 100000: 4315657.8 i/s - 1.14x slower
57
+ bin/bench_n 250000: 4204141.1 i/s - 1.17x slower
58
+ bin/bench_n 500000: 3902748.9 i/s - 1.26x slower
59
+ bin/bench_n 1000000: 3395620.2 i/s - 1.44x slower
60
+ bin/bench_n 2500000: 2659274.8 i/s - 1.84x slower
61
+ bin/bench_n 5000000: 2346630.0 i/s - 2.09x slower
62
+ bin/bench_n 10000000: 2022304.5 i/s - 2.42x slower
63
+
64
+ push + pop (c_dheap)
65
+ bin/bench_n 10000: 7311366.6 i/s
66
+ bin/bench_n 50000: 6737824.5 i/s - 1.09x slower
67
+ bin/bench_n 25000: 6407340.6 i/s - 1.14x slower
68
+ bin/bench_n 100000: 6254396.3 i/s - 1.17x slower
69
+ bin/bench_n 250000: 5917684.5 i/s - 1.24x slower
70
+ bin/bench_n 500000: 5126307.6 i/s - 1.43x slower
71
+ bin/bench_n 1000000: 4403494.1 i/s - 1.66x slower
72
+ bin/bench_n 2500000: 3304088.2 i/s - 2.21x slower
73
+ bin/bench_n 5000000: 2664897.7 i/s - 2.74x slower
74
+ bin/bench_n 10000000: 2137927.6 i/s - 3.42x slower
75
+
@@ -0,0 +1,39 @@
1
+ Calculating -------------------------------------
2
+ bin/bench_n 10000 bin/bench_n 100000 bin/bench_n 1000000 bin/bench_n 10000000
3
+ push + pop (findmin) 40.280M 42.236M 58.956M 204.536M bytes - 1.000 times
4
+ push + pop (bsearch) 40.212M 42.036M 59.012M 204.536M bytes - 1.000 times
5
+ push + pop (rb_heap) 40.264M 41.780M 58.940M 204.364M bytes - 1.000 times
6
+ push + pop (c++ stl) 40.416M 42.268M 56.624M 302.152M bytes - 1.000 times
7
+ push + pop (c_dheap) 40.848M 43.968M 72.888M 568.828M bytes - 1.000 times
8
+
9
+ Comparison:
10
+ push + pop (findmin)
11
+ bin/bench_n 10000: 40280000.0 bytes
12
+ bin/bench_n 100000: 42236000.0 bytes - 1.05x larger
13
+ bin/bench_n 1000000: 58956000.0 bytes - 1.46x larger
14
+ bin/bench_n 10000000: 204536000.0 bytes - 5.08x larger
15
+
16
+ push + pop (bsearch)
17
+ bin/bench_n 10000: 40212000.0 bytes
18
+ bin/bench_n 100000: 42036000.0 bytes - 1.05x larger
19
+ bin/bench_n 1000000: 59012000.0 bytes - 1.47x larger
20
+ bin/bench_n 10000000: 204536000.0 bytes - 5.09x larger
21
+
22
+ push + pop (rb_heap)
23
+ bin/bench_n 10000: 40264000.0 bytes
24
+ bin/bench_n 100000: 41780000.0 bytes - 1.04x larger
25
+ bin/bench_n 1000000: 58940000.0 bytes - 1.46x larger
26
+ bin/bench_n 10000000: 204364000.0 bytes - 5.08x larger
27
+
28
+ push + pop (c++ stl)
29
+ bin/bench_n 10000: 40416000.0 bytes
30
+ bin/bench_n 100000: 42268000.0 bytes - 1.05x larger
31
+ bin/bench_n 1000000: 56624000.0 bytes - 1.40x larger
32
+ bin/bench_n 10000000: 302152000.0 bytes - 7.48x larger
33
+
34
+ push + pop (c_dheap)
35
+ bin/bench_n 10000: 40848000.0 bytes
36
+ bin/bench_n 100000: 43968000.0 bytes - 1.08x larger
37
+ bin/bench_n 1000000: 72888000.0 bytes - 1.78x larger
38
+ bin/bench_n 10000000: 568828000.0 bytes - 13.93x larger
39
+