Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/humanize/filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

from math import log

suffixes = {
"decimal": (
" kB",
Expand Down Expand Up @@ -83,11 +85,7 @@ def naturalsize(
suffix = suffixes["decimal"]

base = 1024 if (gnu or binary) else 1000
if isinstance(value, str):
bytes_ = float(value)
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'

abs_bytes = abs(bytes_)

if abs_bytes == 1 and not gnu:
Expand All @@ -96,10 +94,6 @@ def naturalsize(
if abs_bytes < base:
return f"{int(bytes_)}B" if gnu else f"{int(bytes_)} Bytes"

for i, s in enumerate(suffix, 2):
unit = base**i
if abs_bytes < unit:
break

ret: str = format % (base * (bytes_ / unit)) + s
exp = int(min(log(abs_bytes, base), len(suffix)))
ret: str = format % (bytes_ / (base**exp)) + suffix[exp - 1]
return ret
Loading