-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_calls.py
73 lines (52 loc) · 1.3 KB
/
test_calls.py
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
""" Speed performance tests.
Copyright (c) 2014, eGenix.com Software GmbH; mailto:info@egenix.com
See the documentation for further information on copyrights,
or contact the author. All Rights Reserved.
License: MIT
"""
### Function calls
result = 3
def python_function():
a, b = 1, 2
f = lambda x,y: x+y
for i in xrange(1000):
f(a, b)
return f(a, b)
assert python_function() == result
import operator
def python_c_function():
a, b = 1, 2
f = operator.add
for i in xrange(1000):
f(a, b)
return f(a, b)
assert python_c_function() == result
def python_operators():
a, b = 1, 2
for i in xrange(1000):
a + b
return a + b
assert python_operators() == result
### Method calls
class Class:
def add(self, a, b):
return a + b
def python_method():
test_instance = Class()
a, b = 1, 2
f = test_instance.add
for i in xrange(1000):
f(a, b)
return f(a, b)
assert python_method() == result
def python_method_with_lookup():
test_instance = Class()
a, b = 1, 2
for i in xrange(1000):
test_instance.add(a, b)
return test_instance.add(a, b)
assert python_method_with_lookup() == result
###
if __name__ == '__main__':
import perftools
perftools.time_functions(globals())