Skip to content

Instantly share code, notes, and snippets.

@WizardOfOgz
Last active October 16, 2015 19:01
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 WizardOfOgz/e52bdda95c2154860efc to your computer and use it in GitHub Desktop.
Save WizardOfOgz/e52bdda95c2154860efc to your computer and use it in GitHub Desktop.
require 'benchmark'
puts RUBY_VERSION
phones = ["(216) 375-9240","(518) 462-8200","(215) 434-2915","(714) 778-4011",
"(512) 474-7773","(805) 395-1301","(301) 659-5500","(504) 346-5900","(617) 275-4600",
"(313) 643-9080", "(203) 579-1525", "(716) 854-9100", "(609) 541-5028", "(704) 372-4900",
"(312) 853-3920", "(213) 637-8883", "(216) 696-5422", "(614) 224-3735", "(214) 742-5114",
"(513) 228-8015", "(213) 956-1391", "(919) 373-8633", "(717) 233-9031", "(203) 525-0155",
"(317) 635-0119", "(213) 645-9011", "(201) 333-0250", "(816) 474-1850", "(702) 387-7000",
"(213) 637-8883", "(516) 222-9106", "(213) 628-9902", "(617) 662-2335","(414) 276-1804",
"(612) 340-1100", "(201) 333-0250", "(212) 732-4114", "(804) 625-7495", "(415) 577-0200",
"(312) 857-8800","(402) 592-6000", "(415) 856-1626", "(215) 561-4718", "(602) 257-9128",
"(401) 274-8793", "(212) 626-0226", "(702) 322-1512", "(804) 353-1081", "(313) 775-8755",
"(916) 448-1361", "(312) 675-9370", "(314) 342-8980", "(801) 363-2294", "(512) 226-5664",
"(714) 824-7430", "(714) 231-0846", "(408) 279-4040", "(714) 973-2900", "(413) 781-1602",
"(203) 322-0606", "(315) 422-6341", "(419) 243-4227", "(602) 882-4484", "(918) 584-6030",
"(415) 944-5000", "(202) 861-9000", "(914) 997-1616", "(617) 752-5911"]
n = 100_000
puts "Total Entries: #{phones.size}"
puts "Total Loops: #{n}"
#============================USING Ruby Built in Benchmark====================
Benchmark.bm(10) do |x|
x.report("gsub \D:") { n.times { phones.each{|e| e.to_s.gsub(/\D/, '') }}}
x.report("gsub \D+:") { n.times { phones.each{|e| e.to_s.gsub(/\D+/, '') }}}
x.report("scan \d:") { n.times { phones.each{|e| e.to_s.scan(/\d/).join }}}
x.report("scan \d+:") { n.times { phones.each{|e| e.to_s.scan(/\d+/).join }}}
end
#===================================OUTPUT=====================================
# 2.2.2
# Total Entries: 68
# Total Loops: 100000
# user system total real
# gsub D: 20.780000 0.050000 20.830000 ( 20.841600)
# gsub D+: 20.120000 0.040000 20.160000 ( 20.163322)
# scan d: 39.090000 0.030000 39.120000 ( 39.150630)
# scan d+: 22.210000 0.020000 22.230000 ( 22.238800)
#=========================Using fruity gem====================================
require 'fruity'
compare do
gsub_D { phones.each{|e| e.to_s.gsub(/\D/, '') }}
gsub_Dplus { phones.each{|e| e.to_s.gsub(/\D+/, '') }}
scan_d { phones.each{|e| e.to_s.scan(/\d/).join }}
scan_dplus { phones.each{|e| e.to_s.scan(/\d+/).join }}
end
#===================================OUTPUT=====================================
# Running each test 16 times. Test will take about 1 second.
# gsub_Dplus is similar to gsub_D
# gsub_D is similar to scan_dplus
# scan_dplus is faster than scan_d by 1.9x ± 0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment