-
-
Notifications
You must be signed in to change notification settings - Fork 80
Optimize naturalsize algorithm by using math.log
#253
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
Conversation
| else: | ||
| bytes_ = value | ||
|
|
||
| bytes_ = float(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also faster to simply cast to float rather than do isinstance call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
Changes proposed in this pull request: