public

Benchmark: Mini-parser or Regex?

  • Download Gist
benchmark-mini-parser-vs-regex.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
require 'benchmark'
 
Benchmark.bm do |x|
 
scores = %w{00100300800 32004300X 00(11)34000 0000(15)000X 0000(15)000(13) 10(18)47(11)8(10)3}
 
x.report do
1_000.times do
games = []
scores.each do |s|
 
game = []
cs = s.split('')
p = false
cs.each_with_index do |c,i|
if cs[i-2] == '('
game[-1] += c
elsif !(c == '(' || c == ')')
game << c
end
end
 
games << game
end
puts games.inspect
end
end
 
x.report do
1_000.times do
games = []
scores.each do |s|
games << s.scan(/\(\d+\)|\d/)
end
end
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.