Optimize naturalsize algorithm by using math.log#253
Merged
hugovk merged 2 commits intopython-humanize:mainfrom May 23, 2025
Zaczero:patch-1
Merged
Optimize naturalsize algorithm by using math.log#253hugovk merged 2 commits intopython-humanize:mainfrom Zaczero:patch-1
naturalsize algorithm by using math.log#253hugovk merged 2 commits intopython-humanize:mainfrom
Zaczero:patch-1
Conversation
Zaczero
commented
May 23, 2025
| else: | ||
| bytes_ = value | ||
|
|
||
| bytes_ = float(value) |
Contributor
Author
There was a problem hiding this comment.
It's also faster to simply cast to float rather than do isinstance call.
Member
There was a problem hiding this comment.
Yep:
❯ python --version
Python 3.14.0b1
❯ python -m timeit -s "value = 123.456" "bytes_ = float(value) if isinstance(value, str) else value"
10000000 loops, best of 5: 24.9 nsec per loop
❯ python -m timeit -s "value = 123.456" "bytes_ = float(value)"
20000000 loops, best of 5: 13.7 nsec per loop
❯ python -m timeit -s "value = '123.456'" "bytes_ = float(value) if isinstance(value, str) else value"
5000000 loops, best of 5: 47.7 nsec per loop
❯ python -m timeit -s "value = '123.456'" "bytes_ = float(value)"
10000000 loops, best of 5: 36.2 nsec per loopAlthough in isolation this change would fail:
AssertionError: assert '1000.0 ZB' == '1.0 YB'
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #253 +/- ##
==========================================
- Coverage 99.49% 99.49% -0.01%
==========================================
Files 11 11
Lines 798 794 -4
==========================================
- Hits 794 790 -4
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. 🚀 New features to boost your workflow:
|
hugovk
approved these changes
May 23, 2025
Member
hugovk
left a comment
There was a problem hiding this comment.
Thank you, this is about twice as fast. 🚀
❯ python -m timeit -s "from humanize import naturalsize" "naturalsize(1000**9 * 39)"
500000 loops, best of 5: 426 nsec per loop
❯ python -m timeit -s "from humanize import naturalsize" "naturalsize(1000**9 * 39)"
500000 loops, best of 5: 956 nsec per loop| else: | ||
| bytes_ = value | ||
|
|
||
| bytes_ = float(value) |
Member
There was a problem hiding this comment.
Yep:
❯ python --version
Python 3.14.0b1
❯ python -m timeit -s "value = 123.456" "bytes_ = float(value) if isinstance(value, str) else value"
10000000 loops, best of 5: 24.9 nsec per loop
❯ python -m timeit -s "value = 123.456" "bytes_ = float(value)"
20000000 loops, best of 5: 13.7 nsec per loop
❯ python -m timeit -s "value = '123.456'" "bytes_ = float(value) if isinstance(value, str) else value"
5000000 loops, best of 5: 47.7 nsec per loop
❯ python -m timeit -s "value = '123.456'" "bytes_ = float(value)"
10000000 loops, best of 5: 36.2 nsec per loopAlthough in isolation this change would fail:
AssertionError: assert '1000.0 ZB' == '1.0 YB'
naturalsize algorithm by using math.log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes proposed in this pull request: