Skip to main content

A CLI tool to record how much time it takes to import each dependency.

Project description

benchmark-imports

A CLI tool to record how much time it takes to import each dependency in a Python project. Use it when the import time for your code matters.

example of the program output

Usage

Install:

python3 -m pip install benchmark-imports

Use:

python3 -m benchmark_imports my_module_name

For example, measure import time for numpy:

python3 -m pip install numpy
python3 -m benchmark_imports numpy

Troubleshooting

  • To be able to import your module and all its dependencies, the tool has to be installed in the same environment as the tested module and all its dependencies.
  • Keep in mind that the tool actually imports and executes the given module and all its dependencies. So, don't run it on untrusted code or code that can break something at import time.
  • The tool will report you at the end the list of errors that occured in some modules when importing them but were suppressed by other modules. It doesn't mean something is broken. For example, it can indicate that the library has an optional dependency that it tries to import but just ignores if unavailable. However, that means that on some environment the module will be successfully imported, and so the import time may be different.

Improving import time

When you identified the slow modules, this is what you can do:

  • Decrease coupling. "A little copying is better than a little dependency". For example, if you import numpy only to use a single small function (like numpy.sign), just implement the function by yourself.
  • Use local imports. The best practice is to have imports on the top-level of the file. However, if this is a slow module that is used only in one function which isn't called too often, just move the import into the function body. It won't make the function much slower (well, except when you call it in the first time) because Python caches all imports in sys.modules.
  • Use lazy imports. The idea is about the same as with function-local imports: the module will be actually imported and executed only when you try to use it in the first time. It can be achieved either with deferred-import library or Implementing lazy imports snippet from the importlib documentation.
  • Make type annotations lazy by adding from __future__ import annotations at the beginning of each file (see PEP 563).
  • If something is imported only to be used in type annotations, move the import inside if TYPE_CHECKING block.

And as a general rule, don't optimize something until you prove it is slow. This is why this tool exists.

Module type

For each reported module, the tool will show you one of the following types:

  • root is the original module you benchmark (or one of its parent modules).
  • project is a child module of the root module.
  • dependency is a direct dependency of one of the modules in the project.
  • transitive is a dependency of one of the dependencies.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

benchmark-imports-1.0.0.tar.gz (202.6 kB view details)

Uploaded Source

Built Distribution

benchmark_imports-1.0.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file benchmark-imports-1.0.0.tar.gz.

File metadata

  • Download URL: benchmark-imports-1.0.0.tar.gz
  • Upload date:
  • Size: 202.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.25.1

File hashes

Hashes for benchmark-imports-1.0.0.tar.gz
Algorithm Hash digest
SHA256 630892f436b1925325d701d990b9504629c7f53532ff361ec93f1b4ad4f32ff0
MD5 d9dea0a4a4ed88e40be54919646bf137
BLAKE2b-256 b684571c49b3dfd3bcc693cb373940a15c376be5794cebf273469486dae2be87

See more details on using hashes here.

File details

Details for the file benchmark_imports-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for benchmark_imports-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0162e1071c17d092528743bc685cef4c203ceecf35e58ea4c48ddf88a3aebefa
MD5 625d6d72b7d147879cc03903d1b98147
BLAKE2b-256 120add95ce6e26297f292b51a7da5307f75fddf9039fe842c14d806f36341032

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page