我用Python3.7运行了这段代码,看看当我调用import numpy时会发生什么。
import cProfile, pstats
profiler = cProfile.Profile()
profiler.enable()
import numpy
profiler.disable()
# Get and print table of stats
stats = pstats.Stats(profiler).sort_stats('time')
stats.print_stats()输出的前几行如下所示:
79557 function calls (76496 primitive calls) in 0.120 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
32/30 0.015 0.000 0.017 0.001 {built-in method _imp.create_dynamic}
318 0.015 0.000 0.015 0.000 {built-in method builtins.compile}
115 0.011 0.000 0.011 0.000 {built-in method marshal.loads}
648 0.006 0.000 0.006 0.000 {built-in method posix.stat}
119 0.004 0.000 0.005 0.000 <frozen importlib._bootstrap_external>:914(get_data)
246/244 0.004 0.000 0.007 0.000 {built-in method builtins.__build_class__}
329 0.002 0.000 0.012 0.000 <frozen importlib._bootstrap_external>:1356(find_spec)
59 0.002 0.000 0.002 0.000 {built-in method posix.getcwd}它在builtins.compile上花了很多时间。它正在为pycache创建NumPy的字节码吗?为什么每次都会发生这种情况?
我用的是Mac OS。我真正想要的是加速导入,在我看来compile不应该是必要的。
发布于 2021-04-08 21:55:44
用户L3viathan在评论中指出,numpy的代码包含对compile的显式调用。这就解释了为什么会调用builtins.compile。谢谢!
https://stackoverflow.com/questions/67004127
复制相似问题