src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/builtin_fn_time.d.elv (about) 1 #//skip-test 2 3 # Pauses for at least the specified duration. The actual pause duration depends 4 # on the system. 5 # 6 # This only affects the current Elvish context. It does not affect any other 7 # contexts that might be executing in parallel as a consequence of a command 8 # such as [`peach`](). 9 # 10 # A duration can be a simple [number](language.html#number) (with optional 11 # fractional value) without an explicit unit suffix, with an implicit unit of 12 # seconds. 13 # 14 # A duration can also be a string written as a sequence of decimal numbers, 15 # each with optional fraction, plus a unit suffix. For example, "300ms", 16 # "1.5h" or "1h45m7s". Valid time units are "ns", "us" (or "µs"), "ms", "s", 17 # "m", "h". 18 # 19 # Passing a negative duration causes an exception; this is different from the 20 # typical BSD or GNU `sleep` command that silently exits with a success status 21 # without pausing when given a negative duration. 22 # 23 # See the [Go documentation](https://golang.org/pkg/time/#ParseDuration) for 24 # more information about how durations are parsed. 25 # 26 # Examples: 27 # 28 # ```elvish-transcript 29 # ~> sleep 0.1 # sleeps 0.1 seconds 30 # ~> sleep 100ms # sleeps 0.1 seconds 31 # ~> sleep 1.5m # sleeps 1.5 minutes 32 # ~> sleep 1m30s # sleeps 1.5 minutes 33 # ~> sleep -1 34 # Exception: sleep duration must be >= zero 35 # [tty 8], line 1: sleep -1 36 # ``` 37 fn sleep {|duration| } 38 39 # Runs the callable, and call `$on-end` with the duration it took, as a 40 # number in seconds. If `$on-end` is `$nil` (the default), prints the 41 # duration in human-readable form. 42 # 43 # If `$callable` throws an exception, the exception is propagated after the 44 # on-end or default printing is done. 45 # 46 # If `$on-end` throws an exception, it is propagated, unless `$callable` has 47 # already thrown an exception. 48 # 49 # Example: 50 # 51 # ```elvish-transcript 52 # ~> time { sleep 1 } 53 # 1.006060647s 54 # ~> time { sleep 0.01 } 55 # 1.288977ms 56 # ~> var t = '' 57 # ~> time &on-end={|x| set t = $x } { sleep 1 } 58 # ~> put $t 59 # ▶ (num 1.000925004) 60 # ~> time &on-end={|x| set t = $x } { sleep 0.01 } 61 # ~> put $t 62 # ▶ (num 0.011030208) 63 # ``` 64 # 65 # See also [`benchmark`](). 66 fn time {|&on-end=$nil callable| } 67 68 # Runs `$callable` repeatedly, and reports statistics about how long each run 69 # takes. 70 # 71 # If the `&on-end` callback is not given, `benchmark` prints the average, 72 # standard deviation, minimum and maximum of the time it took to run 73 # `$callback`, and the number of runs. If the `&on-end` callback is given, 74 # `benchmark` instead calls it with a map containing these metrics, keyed by 75 # `avg`, `stddev`, `min`, `max` and `runs`. Each duration value (i.e. all 76 # except `runs`) is given as the number of seconds. 77 # 78 # The number of runs is controlled by `&min-runs` and `&min-time`. The 79 # `$callable` is run at least `&min-runs` times, **and** when the total 80 # duration is at least `&min-time`. 81 # 82 # The `&min-runs` option must be a non-negative integer within the range of the 83 # machine word. 84 # 85 # The `&min-time` option must be a string representing a non-negative duration, 86 # specified as a sequence of decimal numbers with a unit suffix (the numbers 87 # may have fractional parts), such as "300ms", "1.5h" and "1h45m7s". Valid time 88 # units are "ns", "us" (or "µs"), "ms", "s", "m", "h". 89 # 90 # If `&on-run-end` is given, it is called after each call to `$callable`, with 91 # the time that call took, given as the number of seconds. 92 # 93 # If `$callable` throws an exception, `benchmark` terminates and propagates the 94 # exception after the `&on-end` callback (or the default printing behavior) 95 # finishes. The duration of the call that throws an exception is not passed to 96 # `&on-run-end`, nor is it included when calculating the statistics for 97 # `&on-end`. If the first call to `$callable` throws an exception and `&on-end` 98 # is `$nil`, nothing is printed and any `&on-end` callback is not called. 99 # 100 # If `&on-run-end` is given and throws an exception, `benchmark` terminates and 101 # propagates the exception after the `&on-end` callback (or the default 102 # printing behavior) finishes, unless `$callable` has already thrown an 103 # exception 104 # 105 # If `&on-end` throws an exception, the exception is propagated, unless 106 # `$callable` or `&on-run-end` has already thrown an exception. 107 # 108 # Example: 109 # 110 # ```elvish-transcript 111 # ~> benchmark { } 112 # 98ns ± 382ns (min 0s, max 210.417µs, 10119226 runs) 113 # ~> benchmark &on-end={|m| put $m[avg]} { } 114 # ▶ (num 9.8e-08) 115 # ~> benchmark &on-run-end={|d| echo $d} { sleep 0.3 } 116 # 0.301123625 117 # 0.30123775 118 # 0.30119075 119 # 0.300629166 120 # 0.301260333 121 # 301.088324ms ± 234.298µs (min 300.629166ms, max 301.260333ms, 5 runs) 122 # ``` 123 # 124 # See also [`time`](). 125 fn benchmark {|&min-runs=5 &min-time=1s &on-end=$nil &on-run-end=$nil callable| }