Skip to content

Performance ​

An immediate-mode interface rebuilds every frame from the program's state. What does that cost, and how do you keep it low? This page answers with measurements: every number below was measured with scripts/measure_redraw.py running src/examples/figure_bench_redraw.F90 (the data is in docs/data/performance.json, the charts are drawn from it by scripts/perf_charts.py). They are one machine's numbers (WSL2/WSLg, x86-64 with 16 hardware threads, RTX 4070 through D3D12, gfortran 16.2), so read the ratios, not the absolute values. Under WSLg the GPU is reached only with GALLIUM_DRIVER=d3d12, else Mesa renders in software (Installation). macOS and Windows were not measured (Platforms).

In a window ​

010203040ms of CPU per frameidleidle: 1.81 ms of CPU per frame1.81pointer movingpointer moving: 0.90 ms of CPU per frame0.90live data, 10 updates/slive data, 10 updates/s: 2.09 ms of CPU per frame2.09live data, 60 updates/slive data, 60 updates/s: 1.77 ms of CPU per frame1.77100 000-point line, 10/s100 000-point line, 10/s: 4.85 ms of CPU per frame4.851 000 000-point line, 10/s1 000 000-point line, 10/s: 32.8 ms of CPU per frame32.8256² heatmap, 10/s256² heatmap, 10/s: 3.85 ms of CPU per frame3.85512² heatmap, 10/s512² heatmap, 10/s: 9.13 ms of CPU per frame9.13512² texture, 10/s512² texture, 10/s: 2.50 ms of CPU per frame2.503840×2160 texture, 10/s3840×2160 texture, 10/s: 18.2 ms of CPU per frame18.2
Case (window mode)frames/sCPU per frameof which the program's own code
idle2–81.8 ms0.05 ms
pointer moving486 (no vsync on WSLg)0.9 ms0.04 ms
live data, 10 updates/s9.42.1 ms0.08 ms
live data, 60 updates/s441.8 ms0.08 ms
100 000-point line, 10 updates/s9.04.9 ms2.2 ms
1 000 000-point line, 10 updates/s5.233 ms24 ms
256² heatmap, 10 updates/s9.03.9 ms1.5 ms
512² heatmap, 10 updates/s7.89.1 ms5.7 ms
512² texture, 10 updates/s9.32.5 ms0.6 ms
3840 × 2160 texture, 10 updates/s6.918 ms17 ms

"The program's own code" is the time between two next_frame calls: the widget and plot calls, and the data update. Two things follow.

Redrawing an interface is cheap: about 1–2 ms of CPU. What costs is data: a plot's points (ImPlot turns each into triangles every frame) and the pixels you write. So a frame drawn for nothing is not the problem it is in a retained toolkit, but a frame drawn when nothing changed is still wasted, and lazy redraw avoids it:

  • leave lazy redraw on (the default): an idle window draws twice a second, about 0.4 % of one core;
  • data changed by the program inside the loop: app%request_frames(1) in each frame where it changed (chapter 4 of the tutorial);
  • data that changes on its own pace: app%set_lazy(.true., max_wait=s), s the refresh period you want;
  • app%set_lazy(.false.) only for real animation: it draws at the display rate, about 37 % of a core idle.

Computation and the frame loop ​

FIGURE runs in the program's thread: while the program computes, nothing is drawn and no input is read. Keep each frame short (16 ms for 60 frames/s, 100 ms still feels responsive) by advancing the computation a slice per frame, as tut04_live does: a few iterations, then next_frame. Parallelism goes inside the slice (OpenMP, do concurrent, MPI, the GPU), as in the Mandelbrot explorer, which computes its view in parallel between two frames.

Give the user the slice size (iterations per frame, as tut04_live's slider does) or measure it: the frame's cost is the slice plus the 1–2 ms of the interface. Plots read your arrays each frame without copying them (Data in place), so publishing the state every frame costs nothing extra; what costs is how many points the plots draw.

Large data ​

Lines. ImPlot's cost grows with the points drawn, about 0.2 ms per 10 000 points here: 100 000 points add 2 ms to a frame, a million 24 ms. A plot is at most a few thousand pixels wide, so draw at most a few points per pixel: keep one sample in N, or, to keep spikes visible, the minimum and maximum of each group of N consecutive samples (two points per group). A million samples shown 1000 pixels wide need 2000 points, not a million.

Strided data. Decimating in place costs no copy: ip_line(label, y(::10), xscale=10 * dx) reads every tenth sample with ImPlot's stride. Components of an array of derived types, matrix rows and integer abscissae are read in place too, on gfortran, nvfortran and ifx alike (from v0.5.0: nvfortran copied every strided section before), with two exceptions: an item copies x when its stride differs from the ordinates' (or converts it when it is an integer), and gfortran and nvfortran copy a component of an array of derived types before the call. At a million points a stride costs about 7 % of the item, a copy by FIGURE 20 %, the compiler's temporaries of runs%t and runs%cl 40 to 60 % (double at ten million): keep a long series in its own contiguous array (Known issues). With nvfortran 26.9 at ten million points, a matrix row went from 139 to 77 ms per item in v0.5.0, contiguous data take 70 ms (Data in place).

Fields: a texture beats a heatmap past a few hundred cells a side. A heatmap draws each cell as a rectangle of vertices, which ImPlot rebuilds every frame; a texture is one image the GPU scales. At 512 × 512 the heatmap costs 9.1 ms of CPU per frame against 2.5 ms for the texture, and in remote mode 13.5 MB per frame against 1 MB per update. Keep heatmaps for small fields, where cell labels and exact cell boundaries matter; above about 256 × 256 draw the field with ip_field_image: one call colours it through the colormap into a texture, on a linear, log or symlog scale, and colours it again only when it changes (changed=.true., a new range, colormap or scale).

Colouring a field. ip_field_image colours a 1024 × 1024 field in about 3 ms (figure_test_fields, headless: the colouring without the GPU upload; linear and log, real32 and real64 alike). The upload is that of a texture, about 0.3 ms for a million pixels here (2.2 ms for 3840 × 2160). The colours come from the colormap's own table, as a heatmap's do. On a log or symlog scale the transform (log10, asinh) is the cost: FIGURE colours such fields through a second table, indexed by the value's single-precision bits (the trick of Julia's ImPlotExtra.jl) and restricted to the range, so the transform runs 65 536 times instead of once per value. Measured on 1024 × 1024 values (gcc -O2, x86-64), against the same colours computed value by value:

Scale, rangePer valueBit tableLargest difference
linear [0, 1]1.6 ms1.1 ms1/255
linear [300, 301]1.6 ms1.0 ms1/255 (ImPlotExtra's unrestricted table: 255/255)
log [1e-3, 1e3]6.8 ms1.4 ms1/255
log [1, 2]6.8 ms1.4 ms1/255 (unrestricted: 5/255)
symlog [-1e3, 1e3]10.4 ms1.1 ms1/255
symlog [10, 11]7.6 ms1.2 ms1/255 (unrestricted: 26/255)

The largest difference is per channel, one step of the colormap's own 8-bit table. ImPlotExtra indexes its table by the top 16 bits of any float, which spreads its 65 536 bins over every float from -inf to +inf: a range far from 0 (300 to 301 K) falls into a few bins. FIGURE's table covers only the range's own floats, so a narrow range gets as many bins as it has floats. Linear fields stay per value: the table gains 1.2 to 1.6 times there, not worth an approximation. Fields under 131 072 values are coloured per value on every scale (the table costs 65 536 transforms to build).

Remote mode ​

first versionnownow, deflated1 B/s1 KB/s1 MB/s1 GB/sidleidle, first version: 216 KB/sidle, now: 1 B/spointer movingpointer moving, first version: 25.8 MB/spointer moving, now: 3.08 MB/spointer moving, now, deflated: 1.23 MB/slive data, 10 updates/slive data, 10 updates/s, first version: 1.07 MB/slive data, 10 updates/s, now: 709 KB/slive data, 10 updates/s, now, deflated: 282 KB/slive data, 60 updates/slive data, 60 updates/s, first version: 6.62 MB/slive data, 60 updates/s, now: 4.24 MB/slive data, 60 updates/s, now, deflated: 1.69 MB/s100 000-point line, 10/s100 000-point line, 10/s, first version: 545 MB/s100 000-point line, 10/s, now: 54.8 MB/s100 000-point line, 10/s, now, deflated: 13 MB/s256² heatmap, 10/s256² heatmap, 10/s, first version: 242 MB/s256² heatmap, 10/s, now: 37.7 MB/s256² heatmap, 10/s, now, deflated: 9.87 MB/s
Case (remote mode, same machine)first versionnownow, deflated
idle216 KB/s1 byte/s
pointer moving25.8 MB/s3.1 MB/s1.2 MB/s
live data, 10 updates/s1.07 MB/s0.71 MB/s0.28 MB/s
live data, 60 updates/s6.6 MB/s4.2 MB/s1.7 MB/s
100 000-point line, 10 updates/s545 MB/s55 MB/s13 MB/s
256² heatmap, 10 updates/s242 MB/s38 MB/s9.9 MB/s

In remote mode the CPU stays as low as in a window (the browser draws); the network carries the cost. Every frame sent is the whole interface as triangles, so the rules above matter more there, and FIGURE adds its own (see Bandwidth):

  • an unchanged frame is not sent: an idle interface costs nothing, live data one frame per update;
  • frames are capped (init_remote(max_fps=), 60 by default) and one at most is queued: a slow link gets the newest frame, never a backlog;
  • frames travel with 12-byte vertices, and are deflated when the link is the bottleneck (compress='auto'), for about 7 ms of CPU per MB.

On a slow link (an SSH tunnel to a cluster node carried about 9 MB/s in these measurements), what you draw decides the frame rate: live data at 60 updates per second still arrives at 60 frames per second (4.2 MB/s), a 100 000-point line at 5 frames per second deflated. Decimate lines and use textures for fields, as above, and refresh live data at the rate you need, not faster (max_wait, or max_fps for the whole program).

A picture the program saves (app%save_image) is drawn on the CPU in remote and headless modes, about 1 ns a pixel per thread: a few milliseconds for a whole window, paid only on the frame that saves (Pictures without a GPU).

Measure your own ​

  • FIGURE_DEBUG_REDRAW=1 prints, once per second, the frames drawn and where the time went (waiting, building, rendering): redraw diagnostics. Window mode only: app%init reads it when it opens the window, headless and remote modes ignore it.
  • scripts/measure_idle.py measures the CPU of an idle program (measuring idle cost).
  • scripts/measure_redraw.py runs figure_bench_redraw through every case of this page, in a window and in remote mode, and prints the tables (--json writes them for scripts/perf_charts.py). In remote mode it records the stream like a browser would and analyses it: identical frames, the share of vertices, deflate's saving. With --connect HOST:PORT --token TOKEN it records a program served elsewhere, such as one on a cluster node reached through an SSH tunnel (--pointer moves the pointer over it meanwhile). --program runs another build of the benchmark (default exe/examples/figure_bench_redraw), --seconds sets each case's length (default 10), --only names the cases (or their prefixes), --env VAR=value passes variables to the program, --compress sets the remote mode's compression, and --gpu samples the utilisation of GPU 0 with nvidia-smi in window mode. Linux only (the CPU time comes from the kernel's accounting of the finished process).
bash
python3 scripts/measure_redraw.py --env GALLIUM_DRIVER=d3d12 --json results.json
python3 scripts/measure_redraw.py --only "live-10 remote" --compress always

Released under the MIT licence.