Optimize is_List et al. Add a script harness and scripts for benchmarking Python...
[scons.git] / bench / lvars-gvars.py
1 # __COPYRIGHT__
2 #
3 # Functions and data for timing different idioms for fetching a keyword
4 # value from a pair of dictionaries for localand global values.  This was
5 # used to select how to most efficiently expand single $KEYWORD strings
6 # in src/engine/SCons/Subst.py.
7
8 def Func1(var, gvars, lvars):
9     """lvars try:-except:, gvars try:-except:"""
10     for i in IterationList:
11         try:
12             x = lvars[var]
13         except KeyError:
14             try:
15                 x = gvars[var]
16             except KeyError:
17                 x = ''
18
19 def Func2(var, gvars, lvars):
20     """lvars has_key(), gvars try:-except:"""
21     for i in IterationList:
22         if lvars.has_key(var):
23             x = lvars[var]
24         else:
25             try:
26                 x = gvars[var]
27             except KeyError:
28                 x = ''
29
30 def Func3(var, gvars, lvars):
31     """lvars has_key(), gvars has_key()"""
32     for i in IterationList:
33         if lvars.has_key(var):
34             x = lvars[var]
35         elif gvars.has_key(var):
36             x = gvars[var]
37         else:
38             x = ''
39
40 def Func4(var, gvars, lvars):
41     """eval()"""
42     for i in IterationList:
43         try:
44             x = eval(var, gvars, lvars)
45         except NameError:
46             x = ''
47
48 # Data to pass to the functions on each run.  Each entry is a
49 # three-element tuple:
50 #
51 #   (
52 #       "Label to print describing this data run",
53 #       ('positional', 'arguments'),
54 #       {'keyword' : 'arguments'},
55 #   ),
56
57 Data = [
58     (
59         "Neither in gvars or lvars",
60         ('x', {}, {}),
61         {},
62     ),
63     (
64         "Missing from lvars, found in gvars",
65         ('x', {'x':1}, {}),
66         {},
67     ),
68     (
69         "Found in lvars",
70         ('x', {'x':1}, {'x':2}),
71         {},
72     ),
73 ]
74