-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.rake
90 lines (65 loc) · 1.62 KB
/
benchmark.rake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require "benchmark"
require "pcre2"
desc "Run a benchmark to compare PCRE2 vs Ruby's built-in Regexp"
task :benchmark do
def benchmark!(pattern, string)
task = ->(re) {
pos = 0
while matchdata = re.match(string, pos)
pos = matchdata.offset(0)[1] + 1
end
}
GC.disable
Benchmark.bmbm do |benchmark|
ruby_re = Regexp.new(pattern)
pcre2_re = PCRE2::Regexp.new(pattern)
pcre2_re_jit = PCRE2::Regexp.new(pattern).tap(&:jit!)
benchmark.report("Ruby Regexp") do
100000.times { task.call(ruby_re) }
end
GC.start
benchmark.report("PCRE2 Regexp") do
100000.times { task.call(pcre2_re) }
end
GC.start
benchmark.report("PCRE2 Regexp - JIT enhanced") do
100000.times { task.call(pcre2_re_jit) }
end
end
GC.enable
puts
puts
puts
end
puts "Benchmark 1: Small pattern, big string"
puts
pattern = "hello"
string = "abab" * 1000
string += "hello"
string += "abab" * 1000
benchmark!(pattern, string)
puts "Benchmark 2: Big pattern, big string"
puts
pattern = "hello" * 50
string = "abab" * 1000
string += "hello"
string += "abab" * 1000
string += pattern
string += "abab" * 1000
benchmark!(pattern, string)
puts "Benchmark 3: Small pattern, small string"
puts
pattern = "hello"
string = "abababab" + "hello" + "abababab"
benchmark!(pattern, string)
puts "Benchmark 3: Multiple matches"
puts
pattern = "hello"
string = ""
20.times do
string += "abab" * 5
string += "hello"
string += "abab" * 5
end
benchmark!(pattern, string)
end