Skip to content

Instantly share code, notes, and snippets.

@ukstv
Last active December 19, 2015 08:39
Show Gist options
  • Save ukstv/5927119 to your computer and use it in GitHub Desktop.
Save ukstv/5927119 to your computer and use it in GitHub Desktop.
require 'benchmark'
N = 50000
STRING = "asd sda sda sd"
Benchmark.bm do |b|
b.report('gsub, s') do
N.times { STRING.gsub(/\s/, '') }
end
b.report('gsub, s+') do
N.times { STRING.gsub(/\s+/, '') }
end
b.report('gsub, space') do
N.times { STRING.gsub(' ', '') }
end
b.report('split.join') do
N.times { STRING.split.join }
end
b.report('delete') do
N.times { STRING.delete(' ') }
end
end
---------------------------------------
user system total real
gsub, s 0.110000 0.000000 0.110000 ( 0.108990)
gsub, s+ 0.110000 0.000000 0.110000 ( 0.111848)
gsub, space 0.130000 0.000000 0.130000 ( 0.127770)
split.join 0.060000 0.000000 0.060000 ( 0.059390)
delete 0.030000 0.000000 0.030000 ( 0.030618)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment