Masa331 blogging

my-photo

Hi, i'm Premysl Donat and this is my blog about IT and my life in generall. I work at UOL and in my free time.. well, look at my Github.

Have a good one!

Menu

Just some string benchmark

Been thinking how #delete #tr #gsub methods are fast:

'some@email.com'.delete('@.')
'some@email.com'.tr('@.', '')
'some@email.com'.gsub('@', '').gsub('.', '')

Result

method real
#delete (0.008266)
#tr (0.009554)
#gsub (0.026041)

The results will be different on other machine but it looks like #delete is fastest.

Code used for benchmark

require 'benchmark'

Benchmark.bmbm do |x|
   x.report { 10000.times { 'donat@gmail.com'.delete('@.') } }
   x.report { 10000.times { 'donat@gmail.com'.tr('@.', '') } }
   x.report { 10000.times { 'donat@gmail.com'.gsub('@', '').gsub('.', '') } }
end