Skip to content

Instantly share code, notes, and snippets.

@louismullie
Created February 14, 2012 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louismullie/1828593 to your computer and use it in GitHub Desktop.
Save louismullie/1828593 to your computer and use it in GitHub Desktop.
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