File: benchmark_map.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; ansic: 288; makefile: 9; sh: 6
file content (35 lines) | stat: -rwxr-xr-x 581 bytes parent folder | download | duplicates (4)
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
34
35
#!/usr/bin/env ruby

require "benchmark"
require "concurrent"

hash  = {}
map = Concurrent::Map.new

ENTRIES = 10_000

ENTRIES.times do |i|
  hash[i]  = i
  map[i] = i
end

TESTS = 40_000_000
Benchmark.bmbm do |results|
  key = rand(10_000)

  results.report('Hash#[]') do
    TESTS.times { hash[key] }
  end

  results.report('Map#[]') do
    TESTS.times { map[key] }
  end

  results.report('Hash#each_pair') do
    (TESTS / ENTRIES).times { hash.each_pair {|k,v| v} }
  end

  results.report('Map#each_pair') do
    (TESTS / ENTRIES).times { map.each_pair {|k,v| v} }
  end
end