diff --git a/stdlib-candidate/std-rfc/bench.nu b/stdlib-candidate/std-rfc/bench.nu index 3c822ef..aa38956 100644 --- a/stdlib-candidate/std-rfc/bench.nu +++ b/stdlib-candidate/std-rfc/bench.nu @@ -5,17 +5,13 @@ use ./math # convert an integer amount of nanoseconds to a real duration def "from ns" [ - --units: string # units to convert duration to (min, sec, ms, µs, ns) - --sign-digits: int # a number of first non-zero digits to keep (default 4; set 0 to disable rounding) + --sign-digits: int = 4 # a number of first non-zero digits to keep (default 4; set 0 to disable rounding) ] { if $sign_digits == 0 {} else { math significant-digits $sign_digits } | $"($in)ns" | into duration - | if $units != null { - format duration $units - } else {} } # run a piece of `nushell` code multiple times and measure the time of execution. @@ -33,7 +29,6 @@ def "from ns" [ # # > **Note** # > `std bench --pretty` will return a `string`. -# > the `--units` option will convert all durations to strings # # # Examples # measure the performance of simple addition @@ -46,18 +41,9 @@ def "from ns" [ # ╰──────┴───────────╯ # # get a pretty benchmark report -# > std bench {1 + 2} --pretty +# > bench {1 + 2} --pretty # 922ns +/- 2µs 40ns # -# format results as `ms` -# > bench {sleep 0.1sec; 1 + 2} --units ms --rounds 5 -# ╭──────┬───────────╮ -# │ mean │ 104.90 ms │ -# │ min │ 103.60 ms │ -# │ max │ 105.90 ms │ -# │ std │ 0.75 ms │ -# ╰──────┴───────────╯ -# # measure the performance of simple addition with 1ms delay and output each timing # > bench {sleep 1ms; 1 + 2} --rounds 2 --list-timings | table -e # ╭───────┬─────────────────────╮ @@ -75,9 +61,7 @@ export def main [ --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) --pretty # shows the results in human-readable format: " +/- " - --units: string # units to convert duration to (min, sec, ms, µs, ns) --list-timings # list all rounds' timings in a `times` field - --sign-digits: int = 4 # a number of first non-zero digits to keep (default 4; set 0 to disable rounding) ] { let times = seq 1 $rounds | each {|i| @@ -88,16 +72,16 @@ export def main [ if $verbose { print $"($rounds) / ($rounds)" } { - mean: ($times | math avg | from ns --units $units --sign-digits=$sign_digits) - min: ($times | math min | from ns --units $units --sign-digits=$sign_digits) - max: ($times | math max | from ns --units $units --sign-digits=$sign_digits) - std: ($times | math stddev | from ns --units $units --sign-digits=$sign_digits) + mean: ($times | math avg | from ns --sign-digits 4) + min: ($times | math min | from ns --sign-digits 4) + max: ($times | math max | from ns --sign-digits 4) + std: ($times | math stddev | from ns --sign-digits 4) } | if $pretty { $"($in.mean) +/- ($in.std)" } else { if $list_timings { - merge { times: ($times | each { from ns --units $units --sign-digits 0 }) } + merge { times: ($times | each { from ns --sign-digits 0 }) } } else {} } } diff --git a/stdlib-candidate/tests/bench.nu b/stdlib-candidate/tests/bench.nu index 0e2c41f..815b6af 100644 --- a/stdlib-candidate/tests/bench.nu +++ b/stdlib-candidate/tests/bench.nu @@ -1,11 +1,6 @@ use std assert use ../std-rfc bench -export def "test bench-units" [] { - let $test = bench {sleep 0.001sec; 1 + 2} --units ns --rounds 2 | get mean - assert str contains $test " ns" -} - export def "test bench-timings" [] { let $test = bench {1 + 2} --rounds 3 --list-timings | get times | length assert equal $test 3 @@ -15,8 +10,3 @@ export def "test bench-pretty" [] { let $test = (bench {1 + 2} --rounds 3 --pretty) =~ '\d.* \+/- \d' assert equal $test true } - -export def "test bench-sign-digits" [] { - let $test = bench {sleep 1ms} --sign-digits 1 --rounds 5 | get min - assert equal $test 1ms -}