File: benchmark_map.rb

package info (click to toggle)
ruby-concurrent 1.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,136 kB
  • sloc: ruby: 30,875; java: 6,128; ansic: 265; makefile: 26; sh: 19
file content (35 lines) | stat: -rwxr-xr-x 515 bytes parent folder | download
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/ips'
require 'concurrent/map'

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

ENTRIES = 10_000

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

TESTS = 1_000
key = 2732 # srand(0) and rand(10_000)

Benchmark.ips do |results|
  results.report('Hash#[]') do
    hash[key]
  end

  results.report('Map#[]') do
    map[key]
  end

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

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