3
# This is a simple speed benchmark suite for std containers,
4
# to verify their O(n) performance.
5
# It is not part of the standard tests.
13
require 'li_std_speed'
17
def benchmark(f, phigh, sequences)
20
maxlen = sequences.max { |a,b| a.to_s.size <=> b.to_s.size }
21
maxlen = maxlen.to_s.size - 12
22
sequences.each { |s| print "%#{maxlen}s" % "#{s.to_s.sub(/.*::/,'')}" }
24
o_perf = Array.new(sequences.size, 0)
25
last_t = Array.new(sequences.size, nil)
29
sequences.each_with_index do |s, i|
30
cont = s.new((0..n).to_a)
31
Benchmark.benchmark('',0,"%#{maxlen-2}.6r") { |x|
32
t = x.report { f.call(cont) }
33
o_perf[i] += last_t[i] ? (t.real / last_t[i]) : t.real
41
base = 1.0 / Math.log(2.0)
42
sequences.each_with_index do |s, i|
44
# o_perf[i] = 1 if o_perf[i] < 1
45
o_perf[i] = Math.log(o_perf[i]) * base
46
print "%#{maxlen-1}.2f " % o_perf[i]
62
# can't reuse begin since it might get invalidated
63
while it != cont.begin
65
# set returns None, so need to reobtain end
66
it = cont.erase(it) || cont.end
72
size.upto((size<<1) - 1) { |x| cont.push(x) }
77
sequences = [RbVector,RbDeque,RbSet,RbList,
78
RbFloatVector,RbFloatDeque,RbFloatSet,RbFloatList]
80
for f,phigh in [[method(:iterate),n], [method(:insert),n],
82
benchmark(f, phigh, sequences)