-
-
Notifications
You must be signed in to change notification settings - Fork 66
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
Defer imports for faster overall import time: 19 ms -> 5 ms #234
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #234 +/- ##
==========================================
- Coverage 99.49% 99.48% -0.01%
==========================================
Files 11 11
Lines 785 782 -3
==========================================
- Hits 781 778 -3
Misses 4 4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
@hugovk care to share how you measured those things and made graphs? I want to check similarly in my project. |
Sure! Before each test I ran Then I ran
Rather than poring over the log file, you can install https://github.com/nschloe/tuna and run that on the log: That opens the visualisation in the browser. It's a nice tool, you can click and zoom in to see the sub-imports, and click the top to zoom out again. Then change your code to defer some imports and repeat :) See also https://medium.com/alan/how-we-improved-our-python-backend-start-up-time-2c33cd4873c8 Another good tool is https://github.com/sharkdp/hyperfine, which repeatedly runs a command to get an average, for example: ❯ hyperfine --warmup 3 "python3 -c 'import humanize'"
Benchmark 1: python3 -c 'import humanize'
Time (mean ± σ): 21.4 ms ± 0.7 ms [User: 15.9 ms, System: 4.6 ms]
Range (min … max): 20.2 ms … 24.1 ms 127 runs More advanced use allows you to give it two commands and it'll compare them both. You can give different setup commands as well, like tell it to checkout a Git branch before each test. This is for broader use as it includes the Python interpreter startup overhead, whereas |
Defer imports, especially avoid
typing
import for normal use, which is getting slower in 3.14, to make the overall import time ofhumanize
faster.Before: 19 ms
After: 5 ms
Also move mypy dependency to autoupdateable requirements file.