summaryrefslogtreecommitdiffstats
path: root/bin/bench.py
blob: 7d9e2ba0f5de59b6bec0093b6ec9187a20620243 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
#
# A script for timing snippets of Python code.

import time

Now = time.time
#Now = time.clock       # better on Windows, some UNIX/Linux systems

# How many times each snippet of code will be run by the functions
# to gather the time (the "inner loop").

Iterations = 10000

# How many times we'll run each function to collect its aggregate time
# and try to average out timing differences induced by system performance
# (the "outer loop").

Runs = 10

# The functions containing the code snippets to test and compare.
# This script will test any function whose name begins with the string
# "Func" and assumes that they all get passed the same arguments.
# Each function should put its snippet within a block:
#
#       for i in IterationList:
#
# So that (as much as possible) we're testing just the code itself,
# not Python function call overhead.

def Func1(var, gvars, lvars):
    for i in IterationList:
        try:
            x = lvars[var]
        except KeyError:
            try:
                x = gvars[var]
            except KeyError:
                x = ''

def Func2(var, gvars, lvars):
    for i in IterationList:
        if lvars.has_key(var):
            x = lvars[var]
        else:
            try:
                x = gvars[var]
            except KeyError:
                x = ''

def Func3(var, gvars, lvars):
    for i in IterationList:
        if lvars.has_key(var):
            x = lvars[var]
        elif gvars.has_key(var):
            x = gvars[var]
        else:
            x = ''

def Func4(var, gvars, lvars):
    for i in IterationList:
        try:
            x = eval(var, gvars, lvars)
        except NameError:
            x = ''

# Data to pass to the functions on each run.  Each entry is a
# three-element tuple:
#
#   (
#       "Label to print describing this data run",
#       ('positional', 'arguments'),
#       {'keyword' : 'arguments'},
#   ),

Data = [
    (
        "Neither in gvars or lvars",
        ('x', {}, {}),
        {},
    ),
    (
        "Missing from lvars, found in gvars",
        ('x', {'x':1}, {}),
        {},
    ),
    (
        "Found in lvars",
        ('x', {'x':1}, {'x':2}),
        {},
    ),
]



IterationList = [None]

def timer(func, *args, **kw):
    global IterationList
    IterationList = [None] * Iterations
    results = []
    for i in range(Runs):
        start = Now()
        func(*args, **kw)
        finish = Now()
        results.append((finish - start) / Iterations)
    return results

def display(label, results):
    print '    ' + label + ':'
    print '    ',
    for r in results:
        print "%.3f" % (r * 1e6),
    print

def display(label, results):
    total = reduce(lambda x, y: x+y, results, 0.0)
    print "    %8.3f" % ((total * 1e6) / len(results)), ':', label

func_names = filter(lambda x: x.startswith('Func'), locals().keys())
func_names.sort()

for f in func_names:
    func = locals()[f]
    print f + ':'

    for label, args, kw in Data:
        r = apply(timer, (func,)+args, kw)
        display(label, r)