Add test for linear performance

This commit is contained in:
Nobuyoshi Nakada 2023-03-12 18:50:39 +09:00
parent 781222a8bc
commit 7ce4b716bd
No known key found for this signature in database
GPG Key ID: 7CD2805BFA3770C6
Notes: git 2023-03-12 12:12:18 +00:00
2 changed files with 30 additions and 0 deletions

View File

@ -1765,4 +1765,11 @@ class TestRegexp < Test::Unit::TestCase
assert_raise(TypeError) {Regexp.linear_time?(nil)}
assert_raise(TypeError) {Regexp.linear_time?(Regexp.allocate)}
end
def test_linear_performance
pre = ->(n) {[Regexp.new("a?" * n + "a" * n), "a" * n]}
assert_linear_performance(factor: 29, first: 10, max: 1, pre: pre) do |re, s|
re =~ s
end
end
end

View File

@ -738,6 +738,29 @@ eom
end
alias all_assertions_foreach assert_all_assertions_foreach
def assert_linear_performance(factor: 10_000, first: factor, max: 2, pre: ->(n) {n})
n = first
arg = pre.call(n)
tmax = (0..factor).map do
st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
yield arg
(Process.clock_gettime(Process::CLOCK_MONOTONIC) - st)
end.max
1.upto(max) do |i|
i += 1 if first >= factor
n = i * factor
t = tmax * factor
arg = pre.call(n)
message = "[#{i}]: #{n} in #{t}s"
Timeout.timeout(t, nil, message) do
st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
yield arg
assert_operator (Process.clock_gettime(Process::CLOCK_MONOTONIC) - st), :<=, t, message
end
end
end
def diff(exp, act)
require 'pp'
q = PP.new(+"")