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

Speedup x 1.85: Generate __version__ at build to avoid slow importlib.metadata import #200

Merged
merged 2 commits into from
Sep 9, 2024

Conversation

hugovk
Copy link
Member

@hugovk hugovk commented Sep 9, 2024

Like hugovk/tinytext#195.

With Python 3.13.0rc2:

python -X importtime -c "import humanize" 2> import.log && tuna import.log

main

image

PR

image

hyperfine

hyperfine --warmup 32 \
--prepare "git checkout rm-importlib.metadata" 'python3 -c "import humanize # PR"' \
--prepare "git checkout main"                  'python3 -c "import humanize # main"'
Benchmark 1: python3 -c "import humanize # PR"
  Time (mean ± σ):      23.5 ms ±   3.5 ms    [User: 18.0 ms, System: 4.3 ms]
  Range (min … max):    21.8 ms …  43.4 ms    75 runs

  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs.

Benchmark 2: python3 -c "import humanize # main"
  Time (mean ± σ):      43.4 ms ±   2.3 ms    [User: 34.5 ms, System: 7.6 ms]
  Range (min … max):    41.8 ms …  52.9 ms    52 runs

  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs.

Summary
  python3 -c "import humanize # PR" ran
    1.85 ± 0.29 times faster than python3 -c "import humanize # main"

Also move mypy from pre-commit to tox, so it runs when the version file has been generated.

@hugovk hugovk added the changelog: Changed For changes in existing functionality label Sep 9, 2024
@hugovk hugovk merged commit 74dcc60 into python-humanize:main Sep 9, 2024
29 checks passed
@hugovk hugovk deleted the rm-importlib.metadata branch September 9, 2024 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: Changed For changes in existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant