Created
February 14, 2012 17:55
-
-
Save louismullie/1828593 to your computer and use it in GitHub Desktop.
Benchmark: Mini-parser or Regex?
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' | |
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