1#! /usr/bin/env python
2##############################################################################
3#
4# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
5# All Rights Reserved.
6#
7# This software is subject to the provisions of the Zope Public License,
8# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
9# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
10# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
12# FOR A PARTICULAR PURPOSE.
13#
14##############################################################################
15"""Helper program to time compilation and interpretation
16"""
17import getopt
18import sys
19import time
20
21from cStringIO import StringIO
22
23from zope.tal.driver import FILE, compilefile, interpretit
24
25
26def main():
27    count = 10
28    try:
29        opts, args = getopt.getopt(sys.argv[1:], "n:")
30    except getopt.error, msg:
31        print msg
32        sys.exit(2)
33    for o, a in opts:
34        if o == "-n":
35            count = int(a)
36    if not args:
37        args = [FILE]
38    for file in args:
39        print file
40        dummyfile = StringIO()
41        it = timefunc(count, compilefile, file)
42        timefunc(count, interpretit, it, None, dummyfile)
43
44def timefunc(count, func, *args):
45    sys.stderr.write("%-14s: " % func.__name__)
46    sys.stderr.flush()
47    t0 = time.clock()
48    for i in range(count):
49        result = apply(func, args)
50    t1 = time.clock()
51    sys.stderr.write("%6.3f secs for %d calls, i.e. %4.0f msecs per call\n"
52                     % ((t1-t0), count, 1000*(t1-t0)/count))
53    return result
54
55if __name__ == "__main__":
56    main()
57