reperf.py 538 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11
import re
import time

def main():
    s = "\13hello\14 \13world\14 " * 1000
    p = re.compile(r"([\13\14])")
    timefunc(10, p.sub, "", s)
    timefunc(10, p.split, s)
    timefunc(10, p.findall, s)

def timefunc(n, func, *args, **kw):
12
    t0 = time.perf_counter()
13 14
    try:
        for i in range(n):
15
            result = func(*args, **kw)
16 17
        return result
    finally:
18
        t1 = time.perf_counter()
19
        if n > 1:
20 21
            print(n, "times", end=' ')
        print(func.__name__, "%.3f" % (t1-t0), "CPU seconds")
22 23

main()