在正常情况下使用内置 __import__()
Using builtin __import__() in normal cases
这是我检查 __import__()
性能的地方
In [9]: %%timeit
...: math = __import__('math')
...: sqrt = math.sqrt
...: sqrt(7894561230)
...:
The slowest run took 11.16 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 534 ns per loop
In [10]: %%timeit
...: from math import sqrt
...: sqrt(7894561230)
...:
...:
The slowest run took 10.23 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 979 ns per loop
内置 __import__
模块似乎比传统导入方式更快,
那么它是否可以像我使用的那样在代码中使用,或者这样做有什么重大危害,__import__
文档没有说明这样做有任何危害。
但是上面写着
Direct use of __import__()
is rare, except in cases where you want to
import a module whose name is only known at runtime.
所以我的问题是它也可以在正常情况下使用吗?或者它有什么缺点吗?
这里是小"benchmark"。让我们定义两个函数:
def f1():
import sys
def f2():
sys = __import__('sys')
字节码比较:
>>> dis.dis(f1)
5 0 LOAD_CONST 1 (0)
2 LOAD_CONST 0 (None)
4 IMPORT_NAME 0 (sys)
6 STORE_FAST 0 (sys)
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
>>> dis.dis(f2)
8 0 LOAD_GLOBAL 0 (__import__)
2 LOAD_CONST 1 ('sys')
4 CALL_FUNCTION 1
6 STORE_FAST 0 (sys)
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
生成的字节码指令数相同,但不同。
那么时间呢?
>>> timeit.timeit(f1)
0.4096750088112782
>>> timeit.timeit(f2)
0.474958091968411
原来__import__
方式比较慢。
此外,它的可读性远不如经典的 import
语句。
结论:坚持import
。
现在进行一些解释...
我想调用 __import__
比执行 import
语句慢,因为后者生成的字节码被优化了。
看看指令:__import__
的字节码看起来就像任何其他函数调用,带有 CALL_FUNCTION
指令。
另一方面,import
语句导致 IMPORT_NAME
指令,它看起来像是专门用于导入的指令,并且可能由解释器以优化的方式执行。
事实上,第三条指令是两个字节码之间唯一真正的区别。
所以这两个函数的区别在于IMPORT_NAME
和CALL_FUNCTION
.
的区别
这是我检查 __import__()
In [9]: %%timeit
...: math = __import__('math')
...: sqrt = math.sqrt
...: sqrt(7894561230)
...:
The slowest run took 11.16 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 534 ns per loop
In [10]: %%timeit
...: from math import sqrt
...: sqrt(7894561230)
...:
...:
The slowest run took 10.23 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 979 ns per loop
内置 __import__
模块似乎比传统导入方式更快,
那么它是否可以像我使用的那样在代码中使用,或者这样做有什么重大危害,__import__
文档没有说明这样做有任何危害。
但是上面写着
Direct use of
__import__()
is rare, except in cases where you want to import a module whose name is only known at runtime.
所以我的问题是它也可以在正常情况下使用吗?或者它有什么缺点吗?
这里是小"benchmark"。让我们定义两个函数:
def f1():
import sys
def f2():
sys = __import__('sys')
字节码比较:
>>> dis.dis(f1)
5 0 LOAD_CONST 1 (0)
2 LOAD_CONST 0 (None)
4 IMPORT_NAME 0 (sys)
6 STORE_FAST 0 (sys)
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
>>> dis.dis(f2)
8 0 LOAD_GLOBAL 0 (__import__)
2 LOAD_CONST 1 ('sys')
4 CALL_FUNCTION 1
6 STORE_FAST 0 (sys)
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
生成的字节码指令数相同,但不同。 那么时间呢?
>>> timeit.timeit(f1)
0.4096750088112782
>>> timeit.timeit(f2)
0.474958091968411
原来__import__
方式比较慢。
此外,它的可读性远不如经典的 import
语句。
结论:坚持import
。
现在进行一些解释...
我想调用 __import__
比执行 import
语句慢,因为后者生成的字节码被优化了。
看看指令:__import__
的字节码看起来就像任何其他函数调用,带有 CALL_FUNCTION
指令。
另一方面,import
语句导致 IMPORT_NAME
指令,它看起来像是专门用于导入的指令,并且可能由解释器以优化的方式执行。
事实上,第三条指令是两个字节码之间唯一真正的区别。
所以这两个函数的区别在于IMPORT_NAME
和CALL_FUNCTION
.