Mercurial > genshi > genshi-test
comparison examples/basic/kidrun.py @ 73:b0fd16111f2e
Some more performance tweaks.
author | cmlenz |
---|---|
date | Wed, 12 Jul 2006 18:47:39 +0000 |
parents | eca77129518a |
children | c82c002d4c32 |
comparison
equal
deleted
inserted
replaced
72:29f22f4fd583 | 73:b0fd16111f2e |
---|---|
1 from datetime import datetime, timedelta | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
2 import os | 4 import os |
3 import sys | 5 import sys |
6 import time | |
4 | 7 |
5 import kid | 8 import kid |
6 | 9 |
7 def test(): | 10 def test(): |
8 base_path = os.path.dirname(os.path.abspath(__file__)) | 11 base_path = os.path.dirname(os.path.abspath(__file__)) |
10 | 13 |
11 ctxt = dict(hello='<world>', hey='ZYX', bozz=None, | 14 ctxt = dict(hello='<world>', hey='ZYX', bozz=None, |
12 items=['Number %d' % num for num in range(1, 15)], | 15 items=['Number %d' % num for num in range(1, 15)], |
13 prefix='#') | 16 prefix='#') |
14 | 17 |
15 start = datetime.now() | 18 start = time.clock() |
16 template = kid.Template(file='test.kid', **ctxt) | 19 template = kid.Template(file='test.kid', **ctxt) |
17 print ' --> parse stage: ', datetime.now() - start | 20 print ' --> parse stage: %.4f ms' % ((time.clock() - start) * 1000) |
21 | |
22 for output in template.generate(): | |
23 sys.stdout.write(output) | |
24 print | |
18 | 25 |
19 times = [] | 26 times = [] |
20 for i in range(100): | 27 for i in range(1000): |
21 start = datetime.now() | 28 start = time.clock() |
22 for output in template.generate(): | 29 list(template.generate()) |
23 if i == 0: | 30 times.append(time.clock() - start) |
24 sys.stdout.write(output) | 31 sys.stdout.write('.') |
25 if i == 0: | 32 sys.stdout.flush() |
26 print | |
27 else: | |
28 sys.stdout.write('.') | |
29 sys.stdout.flush() | |
30 times.append(datetime.now() - start) | |
31 print | 33 print |
32 | 34 |
33 total_ms = sum([t.seconds * 1000 + t.microseconds for t in times]) | 35 print ' --> render stage: %s ms (average)' % ( |
34 print ' --> render stage: %s (avg), %s (min), %s (max):' % ( | 36 (sum(times) / len(times) * 1000)) |
35 timedelta(microseconds=total_ms / len(times)), | |
36 timedelta(microseconds=min([t.seconds * 1000 + t.microseconds for t in times])), | |
37 timedelta(microseconds=max([t.seconds * 1000 + t.microseconds for t in times]))) | |
38 | 37 |
39 if __name__ == '__main__': | 38 if __name__ == '__main__': |
40 if '-p' in sys.argv: | 39 if '-p' in sys.argv: |
41 import profile, pstats | 40 import profile, pstats |
42 profile.run('test()', '.tmpl_prof') | 41 profile.run('test()', '.tmpl_prof') |