Created
June 27, 2012 12:36
-
-
Save carlosantoniodasilva/3003823 to your computer and use it in GitHub Desktop.
Benchmark string substitution with [] and gsub
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' | |
TIMES = 100000 | |
s = "carlos" | |
Benchmark.bm(10) do |x| | |
x.report('[1...-1]') do | |
TIMES.times do | |
s.encode(s.encoding, :xml => :attr)[1...-1] | |
end | |
end | |
x.report('gsub') do | |
TIMES.times do | |
s.encode(s.encoding, :xml => :attr).gsub(/^"|"$/,'') | |
end | |
end | |
end | |
require 'benchmark/ips' | |
Benchmark.ips do |x| | |
x.report('[1...-1]') do | |
s.encode(s.encoding, :xml => :attr)[1...-1] | |
end | |
x.report('gsub') do | |
s.encode(s.encoding, :xml => :attr).gsub(/^"|"$/,'') | |
end | |
end | |
=begin | |
# BM | |
user system total real | |
[1...-1] 0.870000 0.000000 0.870000 ( 0.951518) | |
gsub 1.480000 0.010000 1.490000 ( 1.554291) | |
# IPS | |
Calculating ------------------------------------- | |
[1...-1] 7734 i/100ms | |
gsub 4642 i/100ms | |
------------------------------------------------- | |
[1...-1] 105354.7 (±2.1%) i/s - 533646 in 5.067577s | |
gsub 63064.5 (±3.3%) i/s - 315656 in 5.010995s | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment