Introduction
When a script, a test run or a page feels slow, --timings shows where the time goes. Put it before the script (or anywhere after one of kscript's own commands), and when the command ends, kscript prints how long each step of the run took, which functions and built-ins took the most time, and the slowest single calls. Nothing in the script changes, and when the flag is left out, kscript records nothing at all, so normal runs are as fast as ever.
Here is a script that works out Fibonacci numbers the slow way, by calling itself:
<!-- fib.kscript -->
<fn name="fib" params="n: int">
<if $n < 2>
<return value="$n" />
</if>
<return value="@fib($n - 1) + @fib($n - 2)" />
</fn>
<let name="words" value="'the quick brown fox'->split(' ')" />
<let name="long" value="$words->filter(fn(word) => $word->count() > 3)" />
<echo value="@fib(20)" />
<echo value="$long->join(', ')" />// fib.kscript
fn fib(n: int) {
if $n < 2 {
return $n
}
return @fib($n - 1) + @fib($n - 2)
}
$words = 'the quick brown fox'->split(' ')
$long = $words->filter(fn(word) => $word->count() > 3)
echo @fib(20)
echo $long->join(', ')Run it with --timings:
kscript --timings fib.kscriptThe script prints 6765 and quick, brown as usual. Then the timings follow, on stderr, so they never mix with what the script prints:
Timings (594.3 ms in all)
┌───────────────┬───────┬──────────┬───────┐
│ Phase │ Times │ Time │ Share │
├───────────────┼───────┼──────────┼───────┤
│ start kscript │ 1 │ 74.0 ms │ 12.4% │
│ native types │ 1 │ 21.4 ms │ 3.6% │
│ packages │ 1 │ 1.14 ms │ 0.2% │
│ read │ 1 │ 413 µs │ 0.1% │
│ parse │ 1 │ 40.5 ms │ 6.8% │
│ imports │ 1 │ 160.2 ms │ 27.0% │
│ check │ 1 │ 43.3 ms │ 7.3% │
│ run │ 1 │ 198.3 ms │ 33.4% │
└───────────────┴───────┴──────────┴───────┘
Most time of their own (own: not counting the calls they made)
┌──────────┬───────────────────┬────────┬──────────┬──────────┬─────────┬──────────┐
│ Kind │ Name │ Calls │ Total │ Own │ Average │ Longest │
├──────────┼───────────────────┼────────┼──────────┼──────────┼─────────┼──────────┤
│ function │ fib fib.kscript:2 │ 21,891 │ 103.3 ms │ 103.3 ms │ 5 µs │ 103.3 ms │
│ element │ statements │ 2 │ 53.0 ms │ 36.9 ms │ 26.5 ms │ 36.3 ms │
│ lambda │ fn fib.kscript:10 │ 4 │ 8.74 ms │ 8.57 ms │ 2.18 ms │ 8.52 ms │
│ method │ list->filter │ 1 │ 14.7 ms │ 6.01 ms │ 14.7 ms │ 14.7 ms │
│ element │ echo │ 2 │ 109.7 ms │ 5.93 ms │ 54.8 ms │ 109.1 ms │
│ method │ string->split │ 1 │ 1.38 ms │ 1.38 ms │ 1.38 ms │ 1.38 ms │
│ method │ list->join │ 1 │ 398 µs │ 398 µs │ 398 µs │ 398 µs │
│ method │ string->count │ 4 │ 167 µs │ 167 µs │ 42 µs │ 165 µs │
└──────────┴───────────────────┴────────┴──────────┴──────────┴─────────┴──────────┘fib ran 21,891 times, and its 103.3 ms is about half of the 198.3 ms the script spent running, so fib is the place to make faster, not split or filter. The numbers on your machine will differ; what matters is how they compare.