基本上,我想知道在每个模块中导入标准库是否比在一个模块中导入一次标准模块并在其他模块中重用它更好/更差。换句话说,我想知道以下两个代码之间是否存在速度/性能差异:
"""
One way - Just importing time module only once
"""
# /usr/bin/python
# mytime.py
import time
def get_time():
return time.time()
# /usr/bin/python
# usingtime.py
import mytime
print (mytime.time() - mytime.time.time())
"""
Another way - importing time module as needed
"""
# /usr/bin/python
# mytime.py
import time
def get_time():
return time.time()
# /usr/bin/python
# usingtime.py
import time
import mytime
print (mytime.time() - time.time())
哪个代码更好?还是真的很重要?