File: Timer.py

package info (click to toggle)
zope-textindexng2 1%3A2.2.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,888 kB
  • ctags: 1,598
  • sloc: ansic: 6,836; python: 6,596; xml: 185; makefile: 137; sh: 41
file content (46 lines) | stat: -rw-r--r-- 1,088 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
###########################################################################
#
# TextIndexNG                The next generation TextIndex for Zope
#
# This software is governed by a license. See
# LICENSE.txt for the terms of this license.
#
###########################################################################

""" 
A simple timer class

$Id: Timer.py,v 1.5 2003/07/09 17:33:47 ajung Exp $
"""

import time

class Timer:
    """ simple timer class for profiling """

    def __init__(self, verbose=1):
        self.ts = time.time()
        self.verbose = verbose
        self.d = {}

    def __call__(self,s):
        diff = time.time() - self.ts
        self.ts = time.time()

        self.d[s] = diff

        if self.verbose:
            print "%s: %5.5lf" % (s,  diff)

    def printStats(self):

        total = 0.0
        for t in self.d.values(): total=total + t

        keys = self.d.keys()
        keys.sort()

        print "total: %5.5lf secs" % total
        for k,v in self.d.items():
            print "%-20s  %5.5lf secs   (%5.2lf %%) " % (k,v,100.0*v/total)