Created
January 19, 2015 15:52
-
-
Save rafbm/9068f377f54b6b7a0915 to your computer and use it in GitHub Desktop.
Ruby String#tr VS String#gsub benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
STRING = 'Haha hah hahaa ahah!!' | |
FIND = 'a' | |
REPLACE = 'o' | |
STRING.tr(FIND, REPLACE) == STRING.gsub(FIND, REPLACE) or abort | |
Benchmark.bmbm do |bm| | |
bm.report '.tr' do | |
1_000_000.times { STRING.tr(FIND, REPLACE) } | |
end | |
bm.report '.gsub' do | |
1_000_000.times { STRING.gsub(FIND, REPLACE) } | |
end | |
end | |
__END__ | |
Rehearsal ----------------------------------------- | |
.tr 0.500000 0.010000 0.510000 ( 0.509202) | |
.gsub 7.230000 0.030000 7.260000 ( 7.305856) | |
-------------------------------- total: 7.770000sec | |
user system total real | |
.tr 0.470000 0.000000 0.470000 ( 0.466244) | |
.gsub 7.110000 0.050000 7.160000 ( 7.201910) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment