File: timing.py

package info (click to toggle)
python-application 1.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 280 kB
  • ctags: 428
  • sloc: python: 1,748; makefile: 6
file content (36 lines) | stat: -rw-r--r-- 834 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
# Copyright (C) 2006-2007 Dan Pascu. See LICENSE for details.
#

"""Measure code execution time for benchmarking and profiling purposes.

Usage:

from application.debug.timing import timer

count = 10000
t = timer(count)
for x in xrange(count):
    ...
t.end(msg="executing loop type 1")

"""

from time import time

class timer(object):
    def __init__(self, count):
        self.count = count
        self.start = time()
    def end(self, duration=True, rate=True, msg=None):
        _duration = time() - self.start
        _rate = self.count/_duration
        if duration:
            format = "time = %(_duration)5.2f sec"
        else:
            format = ""
        if rate:
            format += "; rate = %(_rate)d requests/sec"
        if msg is not None:
            format += "; %(msg)s"
        print format % locals()