Create a gist now

Instantly share code, notes, and snippets.

Benchmark: Mini-parser or Regex?
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment