Skip to content

Instantly share code, notes, and snippets.

@arr-ee
Created August 11, 2013 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arr-ee/6206241 to your computer and use it in GitHub Desktop.
Save arr-ee/6206241 to your computer and use it in GitHub Desktop.
require 'benchmark'
chars = 'abcdeABCDE!@#$%1234 '
res_str = ''
n = 1000
100_000.times do
res_str << chars[rand(chars.length)]
end
Benchmark.bm(15) do |x|
x.report('map downcase'){n.times{res_str.scan(/\w/).map(&:downcase)}}
x.report('scan downcase'){n.times{res_str.scan(/\w/){|w| w.downcase}}}
x.report('one downcase'){n.times{res_str.downcase.scan(/\w/)}}
end
@arr-ee
Copy link
Author

arr-ee commented Aug 11, 2013

                      user     system      total        real
map downcase     26.920000   0.440000  27.360000 ( 27.362158)
scan downcase    25.990000   0.010000  26.000000 ( 25.998966)
one downcase     18.540000   0.350000  18.890000 ( 18.892575)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment