-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportVariants.py
More file actions
174 lines (120 loc) · 3.3 KB
/
ImportVariants.py
File metadata and controls
174 lines (120 loc) · 3.3 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun May 2 00:14:41 2021
@author: maherme
"""
#%%
import sys
for key in sorted(sys.modules.keys()):
print(key)
'cmath' in sys.modules
'cmath' in globals()
#%%
from cmath import exp
'cmath' in globals() # Notice cmath is not in globals
'exp' in globals()
exp
id(exp)
'cmath' in sys.modules
exp(2+2j) # We can call exp, but not cmath.exp()
#%%
cmath = sys.modules['cmath']
'cmath' in globals() # Now cmath is in globals
cmath.exp(2+2j) # We can call cmath methods
#%%
# Notice a problem with import * :
from cmath import *
globals() # Notice all cmath libraries all included now in globals
sin(2+2j) # This will work
from math import *
globals() # Notice some cmath libraries have been overwritten by math libraries
sin(2+2j) # Notice this fails, because is using math library and not cmath
#%%
# You can do:
from cmath import sin as c_sin
from math import sin as r_sin
#%%
from time import perf_counter
from collections import namedtuple
Timings = namedtuple('Timings', 'timing_1 timing_2, abs_diff rel_diff_perc')
def compare_timings(timing1, timing2):
rel_diff = (timing2 - timing1)/timing1 * 100
timings = Timings(round(timing1, 1),
round(timing2, 1),
round(timing2 - timing1, 1),
round(rel_diff, 2))
return timings
#%%
test_repeats = 10_000_000
#%%
# Timing using fully qualified module.symbol
import math
start = perf_counter()
for _ in range(test_repeats):
math.sqrt(2)
end = perf_counter()
elapsed_fully_qualified = end - start
print(f'Elapsed: {elapsed_fully_qualified}')
#%%
# Timing using a directly import symbol name
from math import sqrt
start = perf_counter()
for _ in range(test_repeats):
sqrt(2)
end = perf_counter()
elapsed_direct_symbol = end - start
print(f'Elapsed: {elapsed_direct_symbol}')
#%%
compare_timings(elapsed_fully_qualified, elapsed_direct_symbol)
#%%
# Timing using a function wrapper (fully qualified symbol)
import math
def func():
math.sqrt(2)
start = perf_counter()
for _ in range(test_repeats):
func()
end = perf_counter()
elapsed_func_fully_qualified = end -start
print(f'Elapsed: {elapsed_func_fully_qualified}')
#%%
from math import sqrt
def func():
sqrt(2)
start = perf_counter()
for _ in range(test_repeats):
func()
end = perf_counter()
elapsed_func_direct_qualified = end -start
print(f'Elapsed: {elapsed_func_direct_qualified}')
#%%
compare_timings(elapsed_func_fully_qualified, elapsed_func_direct_qualified)
#%%
# Nested imports
def func():
import math
math.sqrt(2)
start = perf_counter()
for _ in range(test_repeats):
func()
end = perf_counter()
elapsed_nested_fully_qualified = end -start
print(f'Elapsed: {elapsed_nested_fully_qualified}')
#%%
compare_timings(elapsed_func_fully_qualified, elapsed_nested_fully_qualified)
#%%
# Notice this is much slower, because it needs to look up where sqrt is placed,
# and also replace sqrt in local namespaces every time func is called.
def func():
from math import sqrt
sqrt(2)
start = perf_counter()
for _ in range(test_repeats):
func()
end = perf_counter()
elapsed_nested_direct_symbol = end - start
print(f'Elapsed: {elapsed_nested_direct_symbol}')
#%%
compare_timings(elapsed_nested_fully_qualified, elapsed_nested_direct_symbol)
#%%