Skip to content

Instantly share code, notes, and snippets.

@tadman
Created November 4, 2011 15:39
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 tadman/1339615 to your computer and use it in GitHub Desktop.
Save tadman/1339615 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'benchmark'
LETTERS = ('a'..'z').to_a.freeze
def random_customer_name
name = [ ]
(rand(10) + 1).times do
name << LETTERS[rand(LETTERS.length)] * (rand(10) + 3)
end
name.join('.')
end
def customer_list
list = ''
10000.times do
list << random_customer_name + "\n"
end
list
end
sample_customer_lists = [ ]
100.times do
sample_customer_lists << customer_list.split(/\n/)
end
cycles = 10
Benchmark.bm do |bm|
# user system total real
# 0.740000 0.020000 0.760000 ( 0.991042)
bm.report {
cycles.times do
list = sample_customer_lists.pop
list.each { |c| c.gsub!(/\./, ' ') }
end
}
# user system total real
# 0.680000 0.010000 0.690000 ( 1.011136)
bm.report {
cycles.times do
list = sample_customer_lists.pop
list.collect! { |c| c.gsub(/\./, ' ') }
end
}
# user system total real
# 0.490000 0.020000 0.510000 ( 0.628354)
bm.report {
cycles.times do
list = sample_customer_lists.pop
list.collect!{ |c| c.split('.').join(' ') }
end
}
# user system total real
# 0.090000 0.000000 0.090000 ( 0.103968)
bm.report {
cycles.times do
list = sample_customer_lists.pop
list.collect!{ |c| c.tr!('.', ' ') }
end
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment