1 | n/a | import re |
---|
2 | n/a | import time |
---|
3 | n/a | |
---|
4 | n/a | def main(): |
---|
5 | n/a | s = "\13hello\14 \13world\14 " * 1000 |
---|
6 | n/a | p = re.compile(r"([\13\14])") |
---|
7 | n/a | timefunc(10, p.sub, "", s) |
---|
8 | n/a | timefunc(10, p.split, s) |
---|
9 | n/a | timefunc(10, p.findall, s) |
---|
10 | n/a | |
---|
11 | n/a | def timefunc(n, func, *args, **kw): |
---|
12 | n/a | t0 = time.perf_counter() |
---|
13 | n/a | try: |
---|
14 | n/a | for i in range(n): |
---|
15 | n/a | result = func(*args, **kw) |
---|
16 | n/a | return result |
---|
17 | n/a | finally: |
---|
18 | n/a | t1 = time.perf_counter() |
---|
19 | n/a | if n > 1: |
---|
20 | n/a | print(n, "times", end=' ') |
---|
21 | n/a | print(func.__name__, "%.3f" % (t1-t0), "CPU seconds") |
---|
22 | n/a | |
---|
23 | n/a | main() |
---|