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