1998-07-18 05:10:42 +08:00
|
|
|
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):
|
2012-04-29 09:01:20 +08:00
|
|
|
t0 = time.perf_counter()
|
1998-07-18 05:10:42 +08:00
|
|
|
try:
|
|
|
|
for i in range(n):
|
2003-04-06 17:01:11 +08:00
|
|
|
result = func(*args, **kw)
|
1998-07-18 05:10:42 +08:00
|
|
|
return result
|
|
|
|
finally:
|
2012-04-29 09:01:20 +08:00
|
|
|
t1 = time.perf_counter()
|
1998-07-18 05:10:42 +08:00
|
|
|
if n > 1:
|
2007-02-09 13:37:30 +08:00
|
|
|
print(n, "times", end=' ')
|
|
|
|
print(func.__name__, "%.3f" % (t1-t0), "CPU seconds")
|
1998-07-18 05:10:42 +08:00
|
|
|
|
|
|
|
main()
|