Skip to content

Instantly share code, notes, and snippets.

@taku0
Created October 22, 2011 03:40
Show Gist options
  • Save taku0/1305571 to your computer and use it in GitHub Desktop.
Save taku0/1305571 to your computer and use it in GitHub Desktop.
Ruby regex benchmark
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