Created
October 22, 2011 03:40
-
-
Save taku0/1305571 to your computer and use it in GitHub Desktop.
Ruby regex benchmark
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" | |
def test(benchmark, pattern, text, run) | |
benchmark.report(text.length.to_s) do | |
run.times do | |
text = text.gsub(pattern, " ") | |
end | |
end | |
end | |
pattern = /[-_.0-9A-Za-z]+@[-_0-9A-Za-z]+[-_.0-9A-Za-z]+/ | |
RUNS = 2000 | |
Benchmark.bm(6) do |benchmark| | |
test(benchmark, pattern, "abcdefgh" * 10, RUNS) | |
test(benchmark, pattern, "abcdefgh" * 50, RUNS) | |
test(benchmark, pattern, "abcdefgh" * 100, RUNS) | |
test(benchmark, pattern, "abcdefgh" * 500, RUNS) | |
test(benchmark, pattern, "abcdefgh" * 1000, RUNS) | |
test(benchmark, pattern, "abcdefgh" * 5000, RUNS) | |
test(benchmark, pattern, "abcdefgh" * 10000, RUNS) | |
test(benchmark, pattern, "abcdefgh" * 50000, RUNS) | |
test(benchmark, pattern, "abcdefgh" * 100000, RUNS) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment