Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pinzolo/9091422 to your computer and use it in GitHub Desktop.
Save pinzolo/9091422 to your computer and use it in GitHub Desktop.
文字列の Range における include? と cover? のパフォーマンス比較
# coding: utf-8
require 'benchmark'
range = 1.chr("UTF-8")..5000.chr("UTF-8")
try_count = 10000
Benchmark.bm(8) do |bm|
bm.report('include?') do
try_count.times do
range.include?(rand(10000).chr("UTF-8"))
end
end
bm.report('member?') do
try_count.times do
range.member?(rand(10000).chr("UTF-8"))
end
end
bm.report('===') do
try_count.times do
range === rand(10000).chr("UTF-8")
end
end
bm.report('cover?') do
try_count.times do
range.cover?(rand(10000).chr("UTF-8"))
end
end
end
# user system total real
# include? 4.100000 0.010000 4.110000 ( 4.132348)
# member? 4.070000 0.010000 4.080000 ( 4.098094)
# === 3.930000 0.000000 3.930000 ( 3.949908)
# cover? 0.010000 0.000000 0.010000 ( 0.007477)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment