简体   繁体   中英

Why does python call builtins.compile when importing numpy?

I ran this code with python 3.7 to see what happens when I call 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()

The first few lines of output look like this:

         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}

It spends a lot of time on builtins.compile . Is it creating the bytecode for NumPy for pycache? Why would that happen every time?

I'm on Mac OS. What I really want is to speed up the import, and it seems to me that compile should not be necessary.

User L3viathan pointed out in a comment that the code for numpy contains explicit calls to compile . This explains why builtins.compile is getting called. Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM