From 45cea23c896cad741abdaefcb7531f8485cc0704 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk <21087696+oleksandr-pavlyk@users.noreply.github.com> Date: Tue, 14 Jul 2026 16:14:19 -0500 Subject: [PATCH] Fix formatting of negative durations --- python/scripts/nvbench_compare.py | 8 +++----- python/scripts/nvbench_compare_legacy.py | 8 +++----- python/test/test_nvbench_compare.py | 5 ++--- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/python/scripts/nvbench_compare.py b/python/scripts/nvbench_compare.py index f0ad1e2..d6ad337 100644 --- a/python/scripts/nvbench_compare.py +++ b/python/scripts/nvbench_compare.py @@ -2526,15 +2526,13 @@ def format_duration(seconds, *, allow_negative=False, allow_zero=False): ): return "n/a" - if seconds >= 1: + magnitude = abs(seconds) + if magnitude >= 1: multiplier = 1.0 units = "s" - elif seconds >= 1e-3: + elif magnitude >= 1e-3: multiplier = 1e3 units = "ms" - elif seconds >= 1e-6: - multiplier = 1e6 - units = "us" else: multiplier = 1e6 units = "us" diff --git a/python/scripts/nvbench_compare_legacy.py b/python/scripts/nvbench_compare_legacy.py index c51acad..e7adcaf 100644 --- a/python/scripts/nvbench_compare_legacy.py +++ b/python/scripts/nvbench_compare_legacy.py @@ -883,15 +883,13 @@ def format_duration(seconds, *, allow_negative=False, allow_zero=False): ): return "n/a" - if seconds >= 1: + magnitude = abs(seconds) + if magnitude >= 1: multiplier = 1.0 units = "s" - elif seconds >= 1e-3: + elif magnitude >= 1e-3: multiplier = 1e3 units = "ms" - elif seconds >= 1e-6: - multiplier = 1e6 - units = "us" else: multiplier = 1e6 units = "us" diff --git a/python/test/test_nvbench_compare.py b/python/test/test_nvbench_compare.py index f8a582e..d60fea2 100644 --- a/python/test/test_nvbench_compare.py +++ b/python/test/test_nvbench_compare.py @@ -1572,9 +1572,8 @@ def test_format_diff_and_percent_ranges(nvbench_compare): assert nvbench_compare.format_duration(math.inf) == "n/a" assert nvbench_compare.format_duration(-1.0) == "n/a" assert nvbench_compare.format_duration(0.0) == "n/a" - assert ( - nvbench_compare.format_duration(-1.0, allow_negative=True) == "-1000000.000 us" - ) + assert nvbench_compare.format_duration(-1.0, allow_negative=True) == "-1.000 s" + assert nvbench_compare.format_duration(-1e-3, allow_negative=True) == "-1.000 ms" assert nvbench_compare.format_duration(0.0, allow_zero=True) == "0.000 us" assert nvbench_compare.format_duration_range((-12e-6, 8e-6)) == "[-12.00, 8.00] us" assert (