jodosha (owner)

Revisions

gist: 179741 Download_button fork
public
Public Clone URL: git://gist.github.com/179741.git
Embed All Files: show embed
mapped_mget_benchmark.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
require "benchmark"
 
TIMES = 5_000_000
HASH = { "foo" => "1000", "bar" => "2000" }
 
Benchmark.bm(30) do |b|
  b.report("old implementation") do
    TIMES.times do |i|
      keys = %w(foo bar)
      result = HASH.inject({}) do |hash, value|
        key = keys.shift
        value.nil? ? hash : hash.merge(key => value)
      end
    end
  end
  
  b.report("new implementation") do
    TIMES.times do |i|
      result = {}
      keys = %w(foo bar)
      HASH.each do |value|
        key = keys.shift
        result.merge!(key => value) unless value.nil?
      end
    end
  end
end
 
__END__
                                    user     system      total        real
old implementation             43.760000   0.150000  43.910000 ( 44.619332)
new implementation             27.990000   0.080000  28.070000 ( 28.225878)