Module: DHeap::Benchmarks::Randomness

Included in:
DHeap::Benchmarks, Benchmarker, Profiler
Defined in:
lib/d_heap/benchmarks.rb

Overview

moves “rand” outside the benchmarked code, to avoid measuring that too.

Instance Method Summary collapse

Instance Method Details

#default_randomness_sizeObject



20
# File 'lib/d_heap/benchmarks.rb', line 20

def default_randomness_size; 1_000_000 end

#fill_random_vals(target_size = default_randomness_size, io: $stdout) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/d_heap/benchmarks.rb', line 22

def fill_random_vals(target_size = default_randomness_size, io: $stdout)
  @dheap_bm_random_vals ||= []
  count = target_size - @dheap_bm_random_vals.length
  return 0 if count <= 0
  millions = (count / 1_000_000.0).round(3)
  io&.puts "~~~~~~ filling @dheap_bm_random_vals with #{millions}M ~~~~~~"
  io&.flush
  count.times do @dheap_bm_random_vals << rand(0..10_000) end
  @dheap_bm_random_len = @dheap_bm_random_vals.length
  @dheap_bm_random_idx = (((@dheap_bm_random_idx || -1) + 1) % @dheap_bm_random_len)
  nil
end

#random_valObject



35
36
37
38
39
# File 'lib/d_heap/benchmarks.rb', line 35

def random_val
  @dheap_bm_random_vals.fetch(
    @dheap_bm_random_idx = ((@dheap_bm_random_idx + 1) % @dheap_bm_random_len)
  )
end