KScript docs
EN

Timings

Saving the timings to a file

Give --timings a file name and the timings are saved there too. The name picks the format:

Text
kscript --timings timings.json fib.kscript
kscript --timings timings.trace.json fib.kscript
kscript --timings timings.speedscope.json fib.kscript
File name ends in What it holds Open it with
.trace.json Every call, one by one, on a timeline. Perfetto (drag the file in), or chrome://tracing in Chrome.
.speedscope.json Every call, one by one, as a flame graph. speedscope (drag the file in).
any other .json The summary's numbers: every phase, every name with its counts and times, and the 100 slowest calls. Any JSON tool, or a KScript script.

The file name may come straight after --timings, as above, or after =, as in --timings=timings.json. The word after --timings is taken as the file only when it ends in .json, because a script never does. kscript makes the file's folder when it isn't there.

The .json file reads like this, with times in milliseconds (_ms) and the same names the summary shows:

JSON
{
  "version": 1,
  "kscript": "0.60.0",
  "command": "--timings timings.json fib.kscript",
  "started": "2026-12-05T13:26:41.8635774Z",
  "wall_ms": 594.3,
  "phases": [
    { "name": "parse", "count": 1, "total_ms": 40.5, "self_ms": 40.5, "max_ms": 40.5 }
  ],
  "calls": [
    { "kind": "function", "name": "fib", "where": "fib.kscript:2", "count": 21891, "total_ms": 103.3, "self_ms": 103.3, "average_ms": 0.005, "max_ms": 103.3 }
  ],
  "slowest": [
    { "kind": "function", "name": "fib", "where": "fib.kscript:2", "thread": 2, "start_ms": 481.1, "duration_ms": 103.3 }
  ],
  "dropped_events": 0
}

Since it is JSON, a KScript script can read it. This one lists the five names that took the most time of their own:

<!-- slowest.kscript: kscript slowest.kscript timings.json -->
<import package="KScript.IO" />

<let name="timings" value="@read_json($args[0])" />
<for each="call" in="$timings.calls->take(5)">
  <echo>{$call.name}: {$call.self_ms} ms in {$call.count} calls</echo>
</for>
// slowest.kscript: kscript slowest.kscript timings.json
import KScript.IO

$timings = @read_json($args[0])
for call in $timings.calls->take(5) {
  echo "{$call.name}: {$call.self_ms} ms in {$call.count} calls"
}

The trace files keep the first million calls one by one. A run with more still counts every call in the summary and the .json file, and the summary says how many calls the trace left out.