Skip to content

Commit

Permalink
bench Hash#reject, #reject\! and #select
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuck Remes committed Mar 9, 2011
1 parent 6e53d00 commit 6d69963
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
43 changes: 43 additions & 0 deletions benchmark/core/hash/bench_reject.rb
@@ -0,0 +1,43 @@
require 'benchmark'
require 'benchmark/ips'

Benchmark.ips do |x|
hash_strings = {:a => 'a', :b => 'b', :c => 'c', :d => 'd', :e => 'e', :f => 'f', :g => 'g', :h => 'h', :i => 'i', :j => 'j'}
hash_numbers = {:a => -4, :b => -81, :c => 0, :d => 5, :e => 12, :f => -1_000_000, :g => 1, :h => 10, :i => 100, :j => 1000}

x.report "reject string" do |times|
i = 0
while i < times
h = hash_strings.dup
h.reject { |k,v| v < 'd' }
i += 1
end
end

x.report "reject numbers" do |times|
i = 0
while i < times
h = hash_numbers.dup
h.reject { |k,v| v < 10 }
i += 1
end
end

x.report "reject! string" do |times|
i = 0
while i < times
h = hash_strings.dup
h.reject! { |k,v| v < 'd' }
i += 1
end
end

x.report "reject! numbers" do |times|
i = 0
while i < times
h = hash_numbers.dup
h.reject! { |k,v| v < 10 }
i += 1
end
end
end
26 changes: 26 additions & 0 deletions benchmark/core/hash/bench_select.rb
@@ -0,0 +1,26 @@
require 'benchmark'
require 'benchmark/ips'

Benchmark.ips do |x|
hash_strings = {:a => 'a', :b => 'b', :c => 'c', :d => 'd', :e => 'e', :f => 'f', :g => 'g', :h => 'h', :i => 'i', :j => 'j'}
hash_numbers = {:a => -4, :b => -81, :c => 0, :d => 5, :e => 12, :f => -1_000_000, :g => 1, :h => 10, :i => 100, :j => 1000}

x.report "select string" do |times|
i = 0
while i < times
h = hash_strings.dup
h.select { |k,v| v > 'd' }
i += 1
end
end

x.report "select numbers" do |times|
i = 0
while i < times
h = hash_numbers.dup
h.select { |k,v| v > 10 }
i += 1
end
end

end

0 comments on commit 6d69963

Please sign in to comment.