Skip to content

Instantly share code, notes, and snippets.

@justincampbell
Created June 7, 2016 20:37
Show Gist options
  • Save justincampbell/8838a2da58ce0e1415c92f5ed447da34 to your computer and use it in GitHub Desktop.
Save justincampbell/8838a2da58ce0e1415c92f5ed447da34 to your computer and use it in GitHub Desktop.
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
1000000 iterations
user system total real
divide 0.060000 0.000000 0.060000 ( 0.067513)
gtlt 0.060000 0.000000 0.060000 ( 0.054493)
range_include 0.140000 0.000000 0.140000 ( 0.143284)
range_member 0.210000 0.000000 0.210000 ( 0.207428)
string_start_with 0.270000 0.000000 0.270000 ( 0.274578)
string_match 1.130000 0.020000 1.150000 ( 1.160963)
puts RUBY_DESCRIPTION
require 'benchmark'
n = (ARGV[0] || 1_000_000).to_i
puts "#{n} iterations"
Benchmark.bm(18) do |bm|
bm.report :divide do
n.times { 250 / 100 == 2 }
end
bm.report :gtlt do
n.times { 250 >= 200 && 250 <= 299 }
end
bm.report :range_include do
range = (200..299)
n.times { range.include?(250) }
end
bm.report :range_member do
range = (200..299)
n.times { range.member?(250) }
end
bm.report :string_start_with do
two = '2'.freeze
n.times { 250.to_s.start_with?(two) }
end
bm.report :string_match do
regex = /^2/
n.times { regex.match(250.to_s) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment