Skip to content

Instantly share code, notes, and snippets.

@marcotc
Created October 9, 2019 22:28
Show Gist options
  • Save marcotc/09db9af991b0ad3c435573836f86221e to your computer and use it in GitHub Desktop.
Save marcotc/09db9af991b0ad3c435573836f86221e to your computer and use it in GitHub Desktop.
Fastest way to retrive all numbers from a string
# Code mostly adapted from original by Jesus: https://www.ruby-forum.com/t/extract-numbers-from-string/115434/5
require 'benchmark'
n = 100_000
header = 't=1570633834.463123'
Benchmark.bmbm do |x|
x.report('scan') do
n.times {header.scan(/\d/).join}
end
x.report('gsub') do
n.times {header.gsub(/[^\d]/,'')}
end
x.report('gsub2') do
n.times {header.gsub(/\D/, '')}
end
x.report('tr') do
n.times {header.tr('^0-9','')}
end
x.report('delete') do
n.times {header.delete('^0-9')}
end
x.report('split') do
n.times {header.split(/[^\d]/).join}
end
end;
# Memory benchmark using `benchmark-memory` gem: https://github.com/michaelherold/benchmark-memory
require 'benchmark/memory'
Benchmark.memory do |x|
x.report('scan') do
header.scan(/\d/).join
end
x.report('gsub') do
header.gsub(/[^\d]/,'')
end
x.report('gsub2') do
header.gsub(/\D/, '')
end
x.report('tr') do
header.tr('^0-9','')
end
x.report('delete') do
header.delete('^0-9')
end
x.report('split') do
header.split(/[^\d]/).join
end
x.compare!
end;
### OUTPUT ###
Rehearsal ------------------------------------------
scan 0.501514 0.001129 0.502643 ( 0.503412)
gsub 0.212457 0.001135 0.213592 ( 0.213919)
gsub2 0.215123 0.001258 0.216381 ( 0.217076)
tr 0.039926 0.000275 0.040201 ( 0.040221)
delete 0.036174 0.000069 0.036243 ( 0.036440)
split 0.238501 0.001149 0.239650 ( 0.239973)
--------------------------------- total: 1.248710sec
user system total real
scan 0.476308 0.000484 0.476792 ( 0.477103)
gsub 0.210125 0.000944 0.211069 ( 0.211373)
gsub2 0.206936 0.001010 0.207946 ( 0.208079)
tr 0.037802 0.000043 0.037845 ( 0.037923)
delete 0.034158 0.000052 0.034210 ( 0.034314)
split 0.233673 0.001042 0.234715 ( 0.235123)
Calculating -------------------------------------
scan 1.608k memsize ( 320.000 retained)
21.000 objects ( 2.000 retained)
11.000 strings ( 1.000 retained)
gsub 568.000 memsize ( 320.000 retained)
5.000 objects ( 2.000 retained)
3.000 strings ( 1.000 retained)
gsub2 568.000 memsize ( 320.000 retained)
5.000 objects ( 2.000 retained)
3.000 strings ( 1.000 retained)
tr 120.000 memsize ( 0.000 retained)
3.000 objects ( 0.000 retained)
3.000 strings ( 0.000 retained)
delete 80.000 memsize ( 0.000 retained)
2.000 objects ( 0.000 retained)
2.000 strings ( 0.000 retained)
split 648.000 memsize ( 0.000 retained)
9.000 objects ( 0.000 retained)
5.000 strings ( 0.000 retained)
Comparison:
delete: 80 allocated
tr: 120 allocated - 1.50x more
gsub: 568 allocated - 7.10x more
gsub2: 568 allocated - 7.10x more
split: 648 allocated - 8.10x more
scan: 1608 allocated - 20.10x more
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment