-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmarks.py
More file actions
34 lines (29 loc) · 972 Bytes
/
Copy pathbenchmarks.py
File metadata and controls
34 lines (29 loc) · 972 Bytes
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
import time
import re
from pattern import Parser
def benchmark(pattern_str, test_string, iterations=1000):
"""Compare our engine vs Python's re module"""
# Our engine
start = time.time()
for _ in range(iterations):
p = Parser(pattern_str)
matcher = p.matcher(test_string)
result = matcher.match()
our_time = time.time() - start
# Python's re
start = time.time()
pattern = re.compile(pattern_str)
for _ in range(iterations):
result = pattern.fullmatch(test_string)
re_time = time.time() - start
print(f"Pattern: {pattern_str}")
print(f"String: {test_string}")
print(f"Our engine: {our_time:.4f}s")
print(f"Python re: {re_time:.4f}s")
print(f"Ratio: {our_time/re_time:.2f}x slower\n")
# Run benchmarks
print("=== Regex Engine Benchmarks ===\n")
benchmark("abc", "abc")
benchmark("a*b", "aaaaaab")
benchmark("[a-z]+", "hello")
benchmark("\\d{3}-\\d{4}", "123-4567")