Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Measuring import times #37

Open
njzjz opened this issue Jul 29, 2024 · 2 comments
Open

Measuring import times #37

njzjz opened this issue Jul 29, 2024 · 2 comments
Assignees

Comments

@njzjz
Copy link

njzjz commented Jul 29, 2024

I have a package and I hope the first import times are minimal. I used subprocess.check_output to start a new Python process and import modules. However, I just learned that system calls are not included in the total time, so Popen.communicate is fully ignored, as shown below. Is there a recommended way to test the first import time?

image
image

@art049 art049 added the Migrated label Nov 25, 2024 — with Linear
@art049 art049 removed the Migrated label Nov 25, 2024
@art049 art049 changed the title test first import times Measuring import times Feb 19, 2025
@art049 art049 self-assigned this Feb 19, 2025
Copy link
Member

art049 commented Feb 19, 2025

We added a sample #73 measuring the import performance.

The trick is to mock sys.modules to make sure the module is not cached by CPython when running multiple times.

Can you give it a try for your use case ?

@njzjz
Copy link
Author

njzjz commented Feb 20, 2025

Thanks. It works for most cases but does not work for numpy:

      import numpy as np
  /opt/hostedtoolcache/Python/3.12.9/x64/lib/python3.12/site-packages/numpy/__init__.py:114: in <module>
      from numpy.__config__ import show_config
  /opt/hostedtoolcache/Python/3.12.9/x64/lib/python3.12/site-packages/numpy/__config__.py:4: in <module>
      from numpy._core._multiarray_umath import (
  /opt/hostedtoolcache/Python/3.12.9/x64/lib/python3.12/site-packages/numpy/_core/__init__.py:23: in <module>
      from . import multiarray
  /opt/hostedtoolcache/Python/3.12.9/x64/lib/python3.12/site-packages/numpy/_core/multiarray.py:10: in <module>
      from . import overrides
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
  
      """Implementation of __array_function__ overrides from NEP-18."""
      import collections
      import functools
      
      from .._utils import set_module
      from .._utils._inspect import getargspec
  >   from numpy._core._multiarray_umath import (
          add_docstring,  _get_implementing_args, _ArrayFunctionDispatcher)
  E   KeyError: 'numpy.exceptions'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants