-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
gh-146609: Add colour to timeit CLI output
#146610
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
base: main
Are you sure you want to change the base?
Changes from all commits
2d4380c
0f800b3
28e8af9
f6ef701
ff4c493
7e75e56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -268,6 +268,8 @@ def main(args=None, *, _wrap_timer=None): | |
| args = sys.argv[1:] | ||
| import _colorize | ||
| colorize = _colorize.can_colorize() | ||
| theme = _colorize.get_theme(force_color=colorize).timeit | ||
| reset = theme.reset | ||
|
|
||
| try: | ||
| opts, args = getopt.getopt(args, "n:u:s:r:pvh", | ||
|
|
@@ -328,10 +330,13 @@ def main(args=None, *, _wrap_timer=None): | |
| callback = None | ||
| if verbose: | ||
| def callback(number, time_taken): | ||
| msg = "{num} loop{s} -> {secs:.{prec}g} secs" | ||
| plural = (number != 1) | ||
| print(msg.format(num=number, s='s' if plural else '', | ||
| secs=time_taken, prec=precision)) | ||
| s = "" if number == 1 else "s" | ||
| print( | ||
| f"{number} loop{s} " | ||
| f"{theme.arrow}-> " | ||
| f"{theme.timing}{time_taken:.{precision}g} secs{reset}" | ||
| ) | ||
|
|
||
| try: | ||
| number, _ = t.autorange(callback) | ||
| except: | ||
|
|
@@ -362,24 +367,32 @@ def format_time(dt): | |
| return "%.*g %s" % (precision, dt / scale, unit) | ||
|
|
||
| if verbose: | ||
| print("raw times: %s" % ", ".join(map(format_time, raw_timings))) | ||
| raw = ", ".join(map(format_time, raw_timings)) | ||
| print(f"raw times: {theme.timing}{raw}{reset}") | ||
| print() | ||
| timings = [dt / number for dt in raw_timings] | ||
|
|
||
| best = min(timings) | ||
| print("%d loop%s, best of %d: %s per loop" | ||
| % (number, 's' if number != 1 else '', | ||
| repeat, format_time(best))) | ||
|
|
||
| best = min(timings) | ||
| worst = max(timings) | ||
| s = "" if number == 1 else "s" | ||
| print( | ||
| f"{number} loop{s}, best of {repeat}: " | ||
| f"{theme.best}{format_time(best)}{reset} " | ||
| f"{theme.per_loop}per loop{reset}" | ||
| ) | ||
|
|
||
| if worst >= best * 4: | ||
| import warnings | ||
| warnings.warn_explicit("The test results are likely unreliable. " | ||
| "The worst time (%s) was more than four times " | ||
| "slower than the best time (%s)." | ||
| % (format_time(worst), format_time(best)), | ||
| UserWarning, '', 0) | ||
|
|
||
| print(file=sys.stderr) | ||
| warnings.warn_explicit( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this very busy currently, maybe it could just be one colour for warnings?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That seems reasonable to me.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated! |
||
| f"{theme.warning}The test results are likely unreliable. " | ||
| f"The {theme.warning_worst}worst time ({format_time(worst)})" | ||
| f"{theme.warning} was more than four times slower than the " | ||
| f"{theme.warning_best}best time ({format_time(best)})" | ||
| f"{theme.warning}.{reset}", | ||
| UserWarning, "", 0, | ||
| ) | ||
| return None | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add colour to :mod:`timeit` CLI output. Patch by Hugo van Kemenade. |



Uh oh!
There was an error while loading. Please reload this page.