-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing_test.py
100 lines (60 loc) · 1.38 KB
/
timing_test.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
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
import timeit
from collections import namedtuple
def blub(a):
pass
def normal_import():
return namedtuple
def local_import():
from collections import namedtuple
return namedtuple
class lazy_object:
def __getattr__(self, k):
if k == "v":
from collections import namedtuple
self.v = namedtuple
return namedtuple
g1 = lazy_object()
def lazy_import():
return g1.v
class lazy_object_slots:
__slots__ = ("v",)
def __getattr__(self, k):
if k == "v":
from collections import namedtuple
self.v = namedtuple
return namedtuple
g2 = lazy_object_slots()
def lazy_import_slots():
return g2.v
def g3():
global g3
from collections import namedtuple
def g3():
return namedtuple
return namedtuple
def function_import():
return g3()
def g4():
global g4
from collections import namedtuple
def g4(v=namedtuple):
return v
return namedtuple
def function_import_param():
return g4()
def return_none():
return None
results = []
for f in (
local_import,
lazy_import,
lazy_import_slots,
normal_import,
function_import,
function_import_param,
return_none,
):
results.append((timeit.timeit(f), f.__name__))
results.sort()
for time, name in results:
print(f"{name:>25} {time}")