Skip to content

[REQUEST] Time formatting repr from perfplot.bench() output #157

Description

@gabrielfu

Consider posting in https://github.com/nschloe/perfplot/discussions for feedback before raising a feature request.

How would you improve perfplot?

Format the time in perfplot.bench() output in a manner similar to timeit module.

Proposal:
Adding a new function:

def format_time(dt: float, precision: int=3):
    """Formats time to unit. Taken from `timeit` module."""
    units = {"nsec": 1e-9, "usec": 1e-6, "msec": 1e-3, "sec": 1.0}
    scales = [(scale, unit) for unit, scale in units.items()]
    scales.sort(reverse=True)
    for scale, unit in scales:
        if dt >= scale:
            break
    return "%.*g %s" % (precision, dt / scale, unit)

Then, formatting it in PerfplotData.__repr__(self)

lst = [str(n)] + [str(tt) for tt in t]

-            lst = [str(n)] + [str(tt) for tt in t]
+            lst = [str(n)] + [format_time(tt) for tt in t]

What problem does it solved for you?

Right now the output from perfplot.bench() is not very readable:

Code:

out = perfplot.bench(...)
print(out)

Current output:

Overall ---------------------------------------- 100% 0:00:00
Kernels ----------------------------------------   0% 0:00:02
┌────┬────────────────────────┬────────────────────────┬────────────────────────┐
│ n  │ kernel1                │ kernel2                │ kernel3                │
├────┼────────────────────────┼────────────────────────┼────────────────────────┤
│ 4  │ 0.00010070000000000001 │ 4.2600000000000005e-05 │ 1.8000000000000001e-06 │
│ 8  │ 0.0002924              │ 0.0006585              │ 7.8e-06                │
│ 16 │ 0.0010366000000000002  │ 0.0052959              │ 4.1e-05                │
└────┴────────────────────────┴────────────────────────┴────────────────────────┘

Proposed output:

Overall ---------------------------------------- 100% 0:00:00
Kernels ----------------------------------------   0% -:--:--
┌────┬───────────┬───────────┬───────────┐
│ n  │ kernel1   │ kernel2   │ kernel3   │
├────┼───────────┼───────────┼───────────┤
│ 4  │ 158 usec  │ 55.2 usec │ 18.1 usec │
│ 8  │ 386 usec  │ 865 usec  │ 10.9 usec │
│ 16 │ 1.43 msec │ 10.8 msec │ 57.5 usec │
└────┴───────────┴───────────┴───────────┘

Did I help

If I was able to resolve your problem, consider sponsoring my work on perfplot, or buy me a coffee to say thanks.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions