1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +00:00

std bench improvement proposal (#873)

- removed usage of a new external command `math significant-digits`
- rounded precision to 4th digit including (max relative error of 0.05%)
- in `--pretty` I replaced abs `stddev` with relative `coefficient of
variation` (CV)

@amtoine, would you please take a look into my new variant once again? 
Maybe this time it will be good enough? 

<img width="804" alt="image"
src="https://github.com/nushell/nu_scripts/assets/4896754/49e62609-4301-4d5c-b1f5-d2c3355cb33e">
This commit is contained in:
Maxim Uvarov 2024-06-08 22:31:29 +08:00 committed by GitHub
parent d95857c119
commit 398cc82308
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 31 deletions

View file

@ -1,16 +1,12 @@
# A proposal for improving the original `std bench` command by @amtoine # A proposal for improving the original `std bench` command by @amtoine
# https://github.com/nushell/nushell/blob/31f3d2f6642b585f0d88192724723bf0ce330ecf/crates/nu-std/std/mod.nu#L134 # https://github.com/nushell/nushell/blob/31f3d2f6642b585f0d88192724723bf0ce330ecf/crates/nu-std/std/mod.nu#L134
use ./math # round an integer amount of nanoseconds to 4th digit and convert to a real duration
def "from ns" [] {
# convert an integer amount of nanoseconds to a real duration if $in == 0 {0} else { #math log errors on 0
def "from ns" [ math round -p (3 - ($in | math log 10 | math floor)) # rounds to 4th digit including, with maximum realtive err 0.05%
--sign-digits: int = 4 # a number of first non-zero digits to keep (default 4; set 0 to disable rounding) | math round # second math round as a fix for `> 123456 | math round -p -5` = 99999.99999999999
] {
if $sign_digits == 0 {} else {
math significant-digits $sign_digits
} }
| $"($in)ns"
| into duration | into duration
} }
@ -42,26 +38,26 @@ def "from ns" [
# #
# get a pretty benchmark report # get a pretty benchmark report
# > bench {1 + 2} --pretty # > bench {1 + 2} --pretty
# 922ns +/- 2µs 40ns # 716ns ± 76.67%
# #
# measure the performance of simple addition with 1ms delay and output each timing # measure the performance of simple addition with 1ms delay and output each timing
# > bench {sleep 1ms; 1 + 2} --rounds 2 --list-timings | table -e # > bench {sleep 1ms; 1 + 2} --rounds 2 --timings | table -e
# ╭───────┬───────────────────── # ╭───────┬───────────────╮
# │ mean │ 1ms 272µs # │ mean │ 1ms 272µs │
# │ min │ 1ms 259µs # │ min │ 1ms 259µs │
# │ max │ 1ms 285µs # │ max │ 1ms 285µs │
# │ std │ 13µs 370ns # │ std │ 13µs 370ns │
# │ │ ╭─────────────────╮ │ # │ │ ╭───────────╮ │
# │ times │ │ 1ms 285µs 791ns │ │ # │ times │ │ 1ms 285µs │ │
# │ │ │ 1ms 259µs 42ns │ │ # │ │ │ 1ms 259µs │ │
# │ │ ╰─────────────────╯ │ # │ │ ╰───────────╯ │
# ╰───────┴───────────────────── # ╰───────┴───────────────╯
export def main [ export def main [
code: closure # the piece of `nushell` code to measure the performance of code: closure # the piece of `nushell` code to measure the performance of
--rounds (-n): int = 50 # the number of benchmark rounds (hopefully the more rounds the less variance) --rounds (-n): int = 50 # the number of benchmark rounds (hopefully the more rounds the less variance)
--verbose (-v) # be more verbose (namely prints the progress) --verbose (-v) # be more verbose (namely prints the progress)
--pretty # shows the results in human-readable format: "<mean> +/- <stddev>" --pretty # shows the results in human-readable format: "<mean> +/- <stddev>"
--list-timings # list all rounds' timings in a `times` field --timings (-t) # list all rounds' timings in a `times` field
] { ] {
let times = seq 1 $rounds let times = seq 1 $rounds
| each {|i| | each {|i|
@ -72,16 +68,16 @@ export def main [
if $verbose { print $"($rounds) / ($rounds)" } if $verbose { print $"($rounds) / ($rounds)" }
{ {
mean: ($times | math avg | from ns --sign-digits 4) mean: ($times | math avg | from ns)
min: ($times | math min | from ns --sign-digits 4) min: ($times | math min | from ns)
max: ($times | math max | from ns --sign-digits 4) max: ($times | math max | from ns)
std: ($times | math stddev | from ns --sign-digits 4) std: ($times | math stddev | from ns)
} }
| if $pretty { | if $pretty {
$"($in.mean) +/- ($in.std)" $"($in.mean) ± (($in.std / $in.mean) * 100 | math round -p 2)%"
} else { } else {
if $list_timings { if $timings {
merge { times: ($times | each { from ns --sign-digits 0 }) } merge { times: ($times | each { from ns }) }
} else {} } else {}
} }
} }

View file

@ -2,11 +2,11 @@ use std assert
use ../std-rfc bench use ../std-rfc bench
export def "test bench-timings" [] { export def "test bench-timings" [] {
let $test = bench {1 + 2} --rounds 3 --list-timings | get times | length let $test = bench {1 + 2} --rounds 3 --timings | get times | length
assert equal $test 3 assert equal $test 3
} }
export def "test bench-pretty" [] { export def "test bench-pretty" [] {
let $test = (bench {1 + 2} --rounds 3 --pretty) =~ '\d.* \+/- \d' let $test = (bench {1 + 2} --rounds 3 --pretty) =~ '\d.* ± \d'
assert equal $test true assert equal $test true
} }