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
|
require 'benchmark/ips'
require_relative '../../lib/character_set'
if RUBY_VERSION.to_f >= 3.0 && !RUBY_PLATFORM[/java/i]
require 'sorted_set'
else
require 'set'
end
def benchmark(caption: nil, cases: {})
with_stdouts($stdout, string_io = StringIO.new) do
puts caption
Benchmark.ips do |x|
cases.each { |label, callable| x.report(label, &callable) }
x.compare!
end
end
($benchmark_results ||= {})[caption] = string_io.string
end
def with_stdouts(*ios)
old_stdout = $stdout
ios.define_singleton_method(:method_missing) { |*args| each { |io| io.send(*args) } }
ios.define_singleton_method(:respond_to?) { |*args| IO.respond_to?(*args) }
$stdout = ios
yield
ensure
$stdout = old_stdout
end
|