Skip to content

Instantly share code, notes, and snippets.

@inkel
Created July 26, 2013 18:12
Show Gist options
  • Save inkel/6090991 to your computer and use it in GitHub Desktop.
Save inkel/6090991 to your computer and use it in GitHub Desktop.
Benchmark Ruby's Range#cover? vs Enumerable#include?
#! /usr/bin/env ruby
require "benchmark"
SET = (1..100)
N = (ARGV[0] || 1_000_000).to_i
Benchmark.bm(22) do |r|
r.report("#include? - best case") do
N.times { SET.include?(1) }
end
r.report("#include? - worst case") do
N.times { SET.include?(100) }
end
r.report("#include? - avg. case") do
N.times { SET.include?(50) }
end
r.report("#cover? - best case") do
N.times { SET.cover?(1) }
end
r.report("#cover? - worst case") do
N.times { SET.cover?(100) }
end
r.report("#cover? - avg. case") do
N.times { SET.cover?(50) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment