Skip to content

Conversation

@Zaczero
Copy link
Contributor

@Zaczero Zaczero commented May 23, 2025

Changes proposed in this pull request:

  • Optimize naturalsize algorithm by using math.log

else:
bytes_ = value

bytes_ = float(value)
Copy link
Contributor Author

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.

Copy link
Member

@hugovk hugovk May 23, 2025

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.0b1python -m timeit -s "value = 123.456" "bytes_ = float(value) if isinstance(value, str) else value"
10000000 loops, best of 5: 24.9 nsec per looppython -m timeit -s "value = 123.456" "bytes_ = float(value)"
20000000 loops, best of 5: 13.7 nsec per looppython -m timeit -s "value = '123.456'" "bytes_ = float(value) if isinstance(value, str) else value"
5000000 loops, best of 5: 47.7 nsec per looppython -m timeit -s "value = '123.456'" "bytes_ = float(value)"
10000000 loops, best of 5: 36.2 nsec per loop

Although in isolation this change would fail:

AssertionError: assert '1000.0 ZB' == '1.0 YB'

@hugovk hugovk added the changelog: Changed For changes in existing functionality label May 23, 2025
@codecov
Copy link

codecov bot commented May 23, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.49%. Comparing base (b818dbf) to head (a79dafa).
⚠️ Report is 7 commits behind head on main.

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              
Flag Coverage Δ
macos-latest 97.73% <100.00%> (-0.02%) ⬇️
ubuntu-latest 97.73% <100.00%> (-0.02%) ⬇️
windows-latest 95.71% <100.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@hugovk hugovk left a 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 looppython -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)
Copy link
Member

@hugovk hugovk May 23, 2025

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.0b1python -m timeit -s "value = 123.456" "bytes_ = float(value) if isinstance(value, str) else value"
10000000 loops, best of 5: 24.9 nsec per looppython -m timeit -s "value = 123.456" "bytes_ = float(value)"
20000000 loops, best of 5: 13.7 nsec per looppython -m timeit -s "value = '123.456'" "bytes_ = float(value) if isinstance(value, str) else value"
5000000 loops, best of 5: 47.7 nsec per looppython -m timeit -s "value = '123.456'" "bytes_ = float(value)"
10000000 loops, best of 5: 36.2 nsec per loop

Although in isolation this change would fail:

AssertionError: assert '1000.0 ZB' == '1.0 YB'

@hugovk hugovk changed the title Optimize naturalsize algorithm by using math.log Optimize naturalsize algorithm by using math.log May 23, 2025
@hugovk hugovk merged commit bb99238 into python-humanize:main May 23, 2025
34 of 35 checks passed
@Zaczero Zaczero deleted the patch-1 branch May 23, 2025 19:36
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.

2 participants