-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplicit_versus_default_return.py
More file actions
61 lines (43 loc) · 1.63 KB
/
explicit_versus_default_return.py
File metadata and controls
61 lines (43 loc) · 1.63 KB
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
#!/usr/bin/env python
# pylint: disable=C0103
"""Compare explicit return performance versus default return performance."""
import gc
import benchmark
class Benchmark_Explicit_Return(benchmark.Benchmark):
"""Benchmark explicit return."""
each = 1000 # allows for differing number of runs
def setUp(self):
"""Define the number of benchmarks to perform."""
self.size = 25000
self.x = None
def test_explicit_return(self):
"""Allocate the specified number of explicit return calls."""
for _ in range(self.size):
self.explicit_return(1)
def explicit_return(self, dummy):
"""Explicitly return None."""
# Add a circular assignment so that the compiler
# does not optimize away this function call
self.x = dummy
dummy = self.x
return None
class Benchmark_Default_Return(benchmark.Benchmark):
"""Benchmark default return."""
each = 1000 # allows for differing number of runs
def setUp(self):
"""Define the number of benchmarks to perform."""
self.size = 25000
self.x = None
def test_default_return(self):
"""Perform the specified number of default return calls."""
for _ in xrange(self.size):
self.default_return(1)
def default_return(self, dummy):
"""Return default value (naked return)."""
# Add a circular assignment so that the compiler
# does not optimize away this function call
self.x = dummy
dummy = self.x
if __name__ == '__main__':
gc.disable()
benchmark.main(each=5000, format="markdown", numberFormat="%.4g")