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

About me

Hi, i'm Premysl Donat and this is a place where i share things i find interesting.

This is my picture:

Premysl Donat at Balaton lake

I'm a web developer and i work at UOL. I also have a tiny company named Imagine Anything under which i run few side projects. Imagine has a beautiful logo so you better check it out!

I release all of my code under MIT license and it's freely available at Github.

Have a good one, Premek

Follow @Masa331