Created
May 17, 2012 15:21
-
-
Save pmarreck/2719607 to your computer and use it in GitHub Desktop.
Some interesting benchmark results for various ways of doing typical Ruby things
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' | |
REP = 1000000 | |
CACHED_REGEX = /mat[ac]?h/ | |
OBJECT_REGEX = Regexp.new('mat[ac]?h') | |
Benchmark.bm 20 do |x| | |
x.report 'Regex interpolated string' do | |
REP.times do |index| | |
word = 't' | |
/ma#{word}[ac]?h/.match "Some math string" | |
end | |
end | |
x.report 'Regex interpolated string with o flag' do | |
REP.times do |index| | |
word = 't' | |
/ma#{word}[ac]?h/o.match "Some math string" | |
end | |
end | |
x.report 'Regex literal' do | |
REP.times do |index| | |
/mat[ac]?h/.match "Some math string" | |
end | |
end | |
x.report 'Regex cached' do | |
REP.times do |index| | |
CACHED_REGEX.match "Some math string" | |
end | |
end | |
x.report 'Regex instantiation' do | |
REP.times do |index| | |
Regexp.new('mat[ac]?h').match "Some math string" | |
end | |
end | |
x.report 'Regex object' do | |
REP.times do |index| | |
OBJECT_REGEX.match "Some math string" | |
end | |
end | |
x.report 'Regexp out an end =, short string' do | |
REP.times do |index| | |
str = 'somestring=' | |
str.gsub(/\=$/) | |
end | |
end | |
x.report 'Chomp out an end =, short string' do | |
REP.times do |index| | |
str = 'somestring=' | |
str.chomp('=') | |
end | |
end | |
x.report 'Index out an end =, short string' do | |
REP.times do |index| | |
str = 'somestring=' | |
str = str[0..-2] if str[-1]=='=' | |
end | |
end | |
x.report 'Regexp out an end =' do | |
REP.times do |index| | |
str = 'somelongstringsomelongstringsomelongstring=' | |
str.gsub(/\=$/) | |
end | |
end | |
x.report 'Chomp out an end =' do | |
REP.times do |index| | |
str = 'somelongstringsomelongstringsomelongstring=' | |
str.chomp('=') | |
end | |
end | |
x.report 'Index out an end =' do | |
REP.times do |index| | |
str = 'somelongstringsomelongstringsomelongstring=' | |
str = str[0..-2] if str[-1]=='=' | |
end | |
end | |
end | |
# user system total real | |
# Regex interpolated string 7.810000 0.010000 7.820000 ( 7.806186) | |
# Regex interpolated string with o flag 1.550000 0.000000 1.550000 ( 1.548230) | |
# Regex literal 1.360000 0.000000 1.360000 ( 1.361472) | |
# Regex cached 1.350000 0.000000 1.350000 ( 1.354619) | |
# Regex instantiation 6.320000 0.010000 6.330000 ( 6.321500) | |
# Regex object 1.410000 0.000000 1.410000 ( 1.417464) | |
# Regexp out an end =, short string 0.510000 0.000000 0.510000 ( 0.511453) | |
# Chomp out an end =, short string 0.420000 0.000000 0.420000 ( 0.418580) | |
# Index out an end =, short string 0.530000 0.000000 0.530000 ( 0.533264) | |
# Regexp out an end = 0.470000 0.000000 0.470000 ( 0.476832) | |
# Chomp out an end = 0.600000 0.000000 0.600000 ( 0.595940) | |
# Index out an end = 0.690000 0.000000 0.690000 ( 0.696599) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Things that are unexpectedly expensive:
/#{interpolation}/ and Regexp.new, more than /a literal/
String indexing via a Range being the slowest of all
Chomp not actually being much faster at all
Strings over 15 characters seem to result in an extra performance hit