File: bench_thread.rb

package info (click to toggle)
ruby-avl-tree 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 184 kB
  • sloc: ruby: 2,179; makefile: 4
file content (39 lines) | stat: -rw-r--r-- 658 bytes parent folder | download | duplicates (2)
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
36
37
38
39
require 'benchmark'
require 'red_black_tree'

Benchmark.bmbm do |bm|
  bm.report do
    h = ConcurrentRedBlackTree.new
    num = 100000
    max = 1000
    threads = []
    # writers
    2.times do
      threads << Thread.new {
        num.times do
        key = rand(max)
        h[key] = key
        end
      }
    end
    # deleters
    2.times do
      threads << Thread.new {
        num.times do
        key = rand(max)
        h.delete(key)
        end
      }
    end
    # readers
    2.times do
      threads << Thread.new {
        num.times do
        key = rand(max)
        h[key]
        end
      }
    end
    threads.each(&:join)
  end
end