首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么python在导入numpy时会调用builtins.compile?

为什么python在导入numpy时会调用builtins.compile?
EN

Stack Overflow用户
提问于 2021-04-08 20:35:50
回答 1查看 52关注 0票数 0

我用Python3.7运行了这段代码,看看当我调用import numpy时会发生什么。

代码语言:javascript
复制
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()

输出的前几行如下所示:

代码语言:javascript
复制
         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不应该是必要的。

EN

回答 1

Stack Overflow用户

发布于 2021-04-08 21:55:44

用户L3viathan在评论中指出,numpy的代码包含对compile的显式调用。这就解释了为什么会调用builtins.compile。谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67004127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档