Slow frames
ZTK_FRAME_STATS=1 measures each frame of the app in five stages. It prints a line for a frame over 100 ms while the app runs, and a table of the times at the exit of the event loop. Use it when a resize, a scroll, or an animation is slow, to find the stage that uses the time.
| Stage | What it has |
|---|---|
events |
The input events and their bindings, the after and idle scripts, and the <Configure> bindings of a resize. |
style |
The computed style of each widget that changed. |
layout |
The geometry managers: pack, grid, place, and flex. |
paint |
The scene of the frame: the quads of each widget and each canvas item. |
render |
The work of the main thread to give the scene to the renderer. The draw line has the time of the render thread. |
The bug: a slow resize
The chart of examples/debug/slow-resize.tcl draws a curve of 20,000 steps. Its <Configure> binding deletes the items and makes one line item for each step again, at each step of a resize:
proc redraw {} {
.c delete all
foreach {x y} [points $::n] {
.c create line $x $y [expr {$x + 1}] $y -fill #2563eb
}
}
bind .c <Configure> redraw
The script then changes the size of the window in 20 steps, as a person does with the corner of the window. Run it with the stats:
ZTK_FRAME_STATS=1 ztk --headless --exit-after-frames 1 examples/debug/slow-resize.tcl
These are the first lines and the table, on stderr:
ztk: slow frame 116109 us: events 101071, style 1, layout 1, paint 15031, render 5
ztk: slow frame 234534 us: events 217743, style 1, layout 3, paint 16783, render 4
ztk: slow frame 395339 us: events 378248, style 0, layout 1, paint 17086, render 4
frames: 22
frames: 22
late: 22
first frame: 79927 us
events: count 84, p50 1 us, p99 4169782 us, max 4169782 us
style: count 22, p50 1 us, p99 27 us, max 27 us
layout: count 22, p50 2 us, p99 4 us, max 4 us
paint: count 22, p50 17086 us, p99 30566 us, max 30566 us
render: count 22, p50 5 us, p99 4752 us, max 4752 us
frame: p50 395339 us, p99 3392640 us, max 3392640 us
draw: count 22, replaced 0, p50 9193 us, p99 29045 us, max 29045 us
Read the table:
late: 22means that each frame needed more than 16,666 microseconds, the time of one frame at 60 frames in each second.- The stage
eventshas a maximum of 4.2 seconds. That is the<Configure>binding: it runsredraw. paintneeds 17 ms in the middle of the frames, for 20,000 items.
A line ztk: slow frame comes at most once in each second, so a run with many slow frames prints fewer lines than frames.
The fix
The canvas is at most a few hundred pixels wide, so 20,000 points give no better picture than one point for each pixel. Make one line item, and move its points at a resize:
.c create line 0 0 0 0 -fill #2563eb -tags chart
proc redraw {} { .c coords chart [points [winfo width .c]] }
With the argument fix, the same run gives:
late: 0
events: count 84, p50 1 us, p99 2776 us, max 2776 us
paint: count 22, p50 1912 us, p99 2336 us, max 2336 us
frame: p50 4161 us, p99 5079 us, max 5079 us
The times of this page came from a ReleaseFast build of ztk on 2026-11-30, on an Intel Core i7-6700K with Linux, with other builds on the same machine. The times change from run to run and from machine to machine. Compare the stages of one run, and compare two runs on the same machine.
Notes
- The table comes at the exit of the event loop:
--exit-after-frames, or the close of the last window. The commandexitends the app before the table. tools/resize-frame.shandtools/flex-resize.shof the repository time 300 resizes of a real window on a private X server with the same stats.- For the widgets that drew again in each frame, use a trace with the kind
frame(Traces).
tests/debug-examples.sh runs this example and checks the form of the lines.