Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3benchmark.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3benchmark.dtx
4 %
5 % Copyright (C) 2011-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % http://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3kernel bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3benchmark} module\\ Benchmarking^^A
35 % }
36 %
37 % \author{^^A
38 % The \LaTeX{} Project\thanks
39 % {^^A
40 % E-mail:
41 % \href{mailto:[email protected]}
42 % {[email protected]}^^A
43 % }^^A
44 % }
45 %
46 % \date{Released 2026-05-15}
47 %
48 % \maketitle
49 %
50 % \begin{documentation}
51 %
52 % \section{Benchmark}
53 %
54 % \begin{variable}[added = 2025-03-17]{\g_benchmark_duration_target_fp}
55 % This variable (default value: $1$) controls roughly for how long
56 % \cs{benchmark:n} will repeat code to more accurately benchmark it.
57 % The actual duration of one call to \cs{benchmark:n} typically lasts
58 % between half and twice \cs{g_benchmark_duration_target_fp} seconds,
59 % unless of course running the code only once already lasts longer
60 % than this.
61 % \end{variable}
62 %
63 % \begin{variable}[added = 2025-03-17]{\g_benchmark_time_fp, \g_benchmark_ops_fp}
64 % These variables store the results of the most recently run benchmark.
65 % \cs{g_benchmark_time_fp} stores the time \TeX{} took in seconds, and
66 % \cs{g_benchmark_ops_fp} stores the estimated number of elementary
67 % operations. The latter is not set by
68 % \cs{benchmark_tic:}/\cs{benchmark_toc:}.
69 % \end{variable}
70 %
71 % \begin{function}[added = 2025-03-17]{\benchmark_once:n, \benchmark_once_silent:n}
72 % \begin{syntax}
73 % \cs{benchmark_once_silent:n} \Arg{code}
74 % \cs{benchmark_once:n} \Arg{code}
75 % \end{syntax}
76 % Determines the time \cs{g_benchmark_time_fp} (in seconds) taken by
77 % \TeX{} to run the \meta{code}, and an estimated number
78 % \cs{g_benchmark_ops_fp} of elementary operations. In addition,
79 % \cs{benchmark_once:n} prints these values to the terminal. The
80 % \meta{code} is run only once so the time may be quite inaccurate for
81 % fast code.
82 % \end{function}
83 %
84 % \begin{function}[added = 2025-03-17]{\benchmark:n, \benchmark_silent:n}
85 % \begin{syntax}
86 % \cs{benchmark:n} \Arg{code}
87 % \end{syntax}
88 % Determines the time \cs{g_benchmark_time_fp} (in seconds) taken by
89 % \TeX{} to run the \meta{code}, and an estimated number
90 % \cs{g_benchmark_ops_fp} of elementary operations. In addition,
91 % \cs{benchmark:n} prints these values to the terminal. The
92 % \meta{code} may be run many times and not within a group, thus code
93 % with side-effects may cause problems.
94 % \end{function}
95 %
96 % \begin{function}[added = 2025-03-17]{\benchmark_tic:, \benchmark_toc:}
97 % \begin{syntax}
98 % \cs{benchmark_tic:} \meta{slow code} \cs{benchmark_toc:}
99 % \end{syntax}
100 % When it is not possible to run \cs{benchmark:n} (e.g., the code is
101 % part of the execution of a package which cannot be looped) the
102 % tic/toc commands can be used instead to time between two points in
103 % the code. When executed, \cs{benchmark_tic:} will print a line to the
104 % terminal, and \cs{benchmark_toc:} will print a matching line with a
105 % time to indicate the duration between them in seconds.
106 % These commands can be nested.
107 % \end{function}
108 %
109 % \end{documentation}
110 %
111 % \begin{implementation}
112 %
113 % \section{\pkg{l3benchmark} implementation}
114 %
115 % Our working unit is the scaled second, namely $2^{-16}$ seconds.
116 %
117 % \begin{macrocode}
118 %<*code>
119 % \end{macrocode}
120 %
121 % \subsection{Benchmarking code}
122 %
123 % \begin{macrocode}
124 %<@@=benchmark>
125 % \end{macrocode}
126 %
127 % \begin{variable}{\g_benchmark_duration_target_fp}
128 % The benchmark is constrained to take roughly (from half to twice)
129 % \cs{g_benchmark_duration_target_fp} seconds, unless one iteration of
130 % the code takes longer.
131 % \begin{macrocode}
132 \fp_new:N \g_benchmark_duration_target_fp
133 \fp_gset:Nn \g_benchmark_duration_target_fp { 1 }
134 % \end{macrocode}
135 % \end{variable}
136 %
137 % \subsubsection{Raw measurement}
138 %
139 % \begin{variable}{\g_@@_nesting_int}
140 % \begin{macro}{\@@_raw:nN, \@@_raw_aux:N, \@@_raw_end:N}
141 % Store in the given integer variable the time it took to perform a given
142 % piece of code, in scaled seconds. We call \cs{sys_timer:} as
143 % close before and after the code as possible. We store the
144 % intermediate result in a new integer when \cs{@@_raw:nN} is
145 % nested.
146 % \begin{macrocode}
147 \int_new:N \g_@@_nesting_int
148 \cs_new_protected:Npn \@@_raw:nN #1
149 {
150 \int_gincr:N \g_@@_nesting_int
151 \exp_args:Nc \@@_raw_aux:N
152 { g_@@_ \int_use:N \g_@@_nesting_int _int }
153 \@@_raw_aux:
154 #1
155 \@@_raw_end:N
156 }
157 \cs_new_protected:Npn \@@_raw_aux:N #1
158 {
159 \int_gzero_new:N #1
160 \cs_gset_protected:Npn \@@_raw_aux: { \int_gset:Nn #1 { \sys_timer: } }
161 }
162 \cs_new_protected:Npn \@@_raw_end:N #1
163 {
164 \int_gset:Nn #1
165 {
166 \sys_timer: -
167 \int_use:c { g_@@_ \int_use:N \g_@@_nesting_int _int }
168 }
169 \int_gdecr:N \g_@@_nesting_int
170 }
171 % \end{macrocode}
172 % \end{macro}
173 % \end{variable}
174 %
175 % \begin{macro}{\@@_raw_replicate:nnN, \@@_tmp:w}
176 % Here, we wish to measure the time it takes for the piece of code
177 % |#2| to be run |#1| times, and store the result in the
178 % integer~|#3|.
179 %
180 % If the number of copies required is large, defining \cs{@@_tmp:w}
181 % would exhaust \TeX{}'s main memory. In that case, we replicate
182 % $|#1|/5000$ times the given code before passing it to the main call
183 % to \cs{@@_tmp:w}. Of course the division rounds to an integer, so
184 % that step introduces a relative error of order at most
185 % $5000/500000$, less than many other sources of variability.
186 %
187 % We subtract the time for another call to \cs{@@_tmp:w}, with the
188 % same arguments (to capture the time it takes to read the argument)
189 % but empty expansion.
190 % \begin{macrocode}
191 \cs_new_eq:NN \@@_tmp:w ?
192 \cs_new_protected:Npn \@@_raw_replicate:nnN #1
193 {
194 \int_compare:nNnTF {#1} > { 500000 }
195 { \@@_raw_replicate_large:nnN {#1} }
196 { \@@_raw_replicate_small:nnN {#1} }
197 }
198 \cs_new_protected:Npn \@@_raw_replicate_large:nnN #1#2
199 {
200 \cs_set:Npe \@@_tmp:w ##1 { \prg_replicate:nn { 5000 } {##1} }
201 \exp_args:Nno \@@_raw_replicate:nnN { #1 / 5000 }
202 { \@@_tmp:w {#2} }
203 }
204 \cs_new_protected:Npn \@@_raw_replicate_small:nnN #1#2
205 {
206 \cs_set:Npe \@@_tmp:w ##1##2 { \prg_replicate:nn {#1} {##1} }
207 \@@_raw:nN { \@@_tmp:w {#2} { } } \g_@@_time_int
208 \exp_args:No \@@_raw_replicate_aux:nnN
209 { \int_use:N \g_@@_time_int } {#2}
210 }
211 \cs_new_protected:Npn \@@_raw_replicate_aux:nnN #1#2#3
212 {
213 \@@_raw:nN { \@@_tmp:w { } {#2} } \g_@@_time_int
214 \int_gset:Nn #3 { #1 - \g_@@_time_int }
215 \cs_set_eq:NN \@@_tmp:w \prg_do_nothing:
216 }
217 % \end{macrocode}
218 % \end{macro}
219 %
220 % \subsubsection{Main benchmarking}
221 %
222 % \begin{variable}{\g_benchmark_time_fp, \g_benchmark_ops_fp}
223 % Functions such as \cs{benchmark:n} store the measured time in
224 % \cs{g_benchmark_time_fp} (in seconds) and the estimated number of
225 % operations in \cs{g_benchmark_ops_fp}.
226 % \begin{macrocode}
227 \fp_new:N \g_benchmark_time_fp
228 \fp_new:N \g_benchmark_ops_fp
229 % \end{macrocode}
230 % \end{variable}
231 %
232 % \begin{variable}{\g_@@_duration_int}
233 % A conversion of \cs{g_benchmark_duration_target_fp} seconds into scaled seconds.
234 % \begin{macrocode}
235 \int_new:N \g_@@_duration_int
236 % \end{macrocode}
237 % \end{variable}
238 %
239 % \begin{variable}{\g_@@_time_int, \g_@@_time_a_int, \g_@@_time_b_int, \g_@@_time_c_int, \g_@@_time_d_int}
240 % These variables hold the time for running a piece of code, as an
241 % integer in scaled seconds.
242 % \begin{macrocode}
243 \int_new:N \g_@@_time_int
244 \int_new:N \g_@@_time_a_int
245 \int_new:N \g_@@_time_b_int
246 \int_new:N \g_@@_time_c_int
247 \int_new:N \g_@@_time_d_int
248 % \end{macrocode}
249 % \end{variable}
250 %
251 % \begin{variable}{\g_@@_repeat_int}
252 % Holds the number of times that the piece of code was
253 % repeated when timing.
254 % \begin{macrocode}
255 \int_new:N \g_@@_repeat_int
256 % \end{macrocode}
257 % \end{variable}
258 %
259 % \begin{variable}{\g_@@_code_tl}
260 % Holds the piece of code to repeat.
261 % \begin{macrocode}
262 \tl_new:N \g_@@_code_tl
263 % \end{macrocode}
264 % \end{variable}
265 %
266 % \begin{macro}{\benchmark_once:n, \benchmark_once_silent:n}
267 % Convert the raw time from scaled seconds to seconds, and convert to
268 % a number of operations. It is important to measure the elementary
269 % operation before running the user code because both measurements use
270 % the same temporary variables.
271 % \begin{macrocode}
272 \cs_new_protected:Npn \benchmark_once:n #1
273 {
274 \benchmark_once_silent:n {#1}
275 \@@_display:
276 }
277 \cs_new_protected:Npn \benchmark_once_silent:n #1
278 {
279 \@@_measure_op:
280 \@@_raw:nN {#1} \g_@@_time_int
281 \fp_gset:Nn \g_benchmark_time_fp { \g_@@_time_int / 65536 }
282 \fp_gset:Nn \g_benchmark_ops_fp { \g_benchmark_time_fp / \g_@@_one_op_fp }
283 }
284 % \end{macrocode}
285 % \end{macro}
286 %
287 % \begin{macro}{\benchmark:n}
288 % After setting up some variables the work is done by \cs{@@_aux:}.
289 % \begin{macrocode}
290 \cs_new_protected:Npn \benchmark:n #1
291 {
292 \benchmark_silent:n {#1}
293 \@@_display:
294 }
295 \cs_new_protected:Npn \benchmark_silent:n #1
296 {
297 \@@_measure_op:
298 \tl_gset:Nn \g_@@_code_tl {#1}
299 \@@_aux:
300 \fp_gset:Nn \g_benchmark_ops_fp { \g_benchmark_time_fp / \g_@@_one_op_fp }
301 }
302 % \end{macrocode}
303 % \end{macro}
304 %
305 % \begin{macro}{\@@_aux:}
306 % The main timing function. First time the user code once. If that
307 % took more than half the allotted time (\cs{g_@@_duration_int}) we're
308 % done. If that took much less, repeatedly quadruple the number of
309 % copies until it takes a reasonable amount of time. Once we reach a
310 % reasonable time (or we risk an overflow), compute a number of times
311 % that can fit in one quarter of the allotted time and measure that
312 % four times. To save time we reuse the result of the first pass if
313 % \cs{g_@@_repeat_int} is one. Once we have four results, find the
314 % smallest, divided by $65536$ and by the number of repetitions, and
315 % display that.
316 % \begin{macrocode}
317 \cs_new_protected:Npn \@@_aux:
318 {
319 \int_gset:Nn \g_@@_repeat_int { 1 }
320 \@@_raw:nN { \g_@@_code_tl } \g_@@_time_int
321 \int_compare:nNnF \g_@@_time_int < { \g_@@_duration_int / 2 }
322 { \prg_break: }
323 \bool_until_do:nn
324 {
325 \int_compare_p:nNn \g_@@_time_int > { \g_@@_duration_int / 32 }
326 || \int_compare_p:nNn \g_@@_repeat_int > { \c_max_int / 4 }
327 }
328 {
329 \int_gset:Nn \g_@@_repeat_int { 4 * \g_@@_repeat_int }
330 \@@_run:N \g_@@_time_int
331 }
332 \int_gset:Nn \g_@@_repeat_int
333 {
334 \fp_to_int:n
335 {
336 max ( 1 , min ( \c_max_int ,
337 \g_@@_duration_int * \g_@@_repeat_int /
338 \int_eval:n { 4 * \g_@@_time_int } ) )
339 }
340 }
341 \int_compare:nNnTF \g_@@_repeat_int = 1
342 { \int_gset_eq:NN \g_@@_time_a_int \g_@@_time_int }
343 { \@@_run:N \g_@@_time_a_int }
344 \@@_run:N \g_@@_time_b_int
345 \@@_run:N \g_@@_time_c_int
346 \@@_run:N \g_@@_time_d_int
347 \int_gset:Nn \g_@@_time_int
348 {
349 \int_min:nn
350 { \int_min:nn \g_@@_time_a_int \g_@@_time_b_int }
351 { \int_min:nn \g_@@_time_c_int \g_@@_time_d_int }
352 }
353 \prg_break_point:
354 \int_compare:nNnT \g_@@_time_int < 3 { \int_gzero:N \g_@@_time_int }
355 \fp_gset:Nn \g_benchmark_time_fp
356 { \g_@@_time_int / \g_@@_repeat_int / 65536 }
357 }
358 \cs_new_protected:Npn \@@_run:N
359 { \exp_args:NNo \@@_raw_replicate:nnN \g_@@_repeat_int { \g_@@_code_tl } }
360 % \end{macrocode}
361 % \end{macro}
362 %
363 % \subsubsection{Display}
364 %
365 % \begin{variable}{\g_@@_one_op_fp}
366 % Time for one operation.
367 % \begin{macrocode}
368 \fp_new:N \g_@@_one_op_fp
369 % \end{macrocode}
370 % \end{variable}
371 %
372 % \begin{macro}{\@@_measure_op:}
373 % Measure one arbitrary single operation (which we put in \cs{g_@@_code_tl}).
374 % This uses a common auxiliary \cs{@@_aux:} with the main benchmark function.
375 % \begin{macrocode}
376 \cs_new_protected:Npn \@@_measure_op:
377 {
378 \int_gset:Nn \g_@@_duration_int
379 { \fp_to_int:n { 65536 * \g_benchmark_duration_target_fp } / 4 }
380 \tl_gset:Nn \g_@@_code_tl
381 { \int_gadd:Nn \g_@@_duration_int { 0 } }
382 \@@_aux:
383 \fp_gset:Nn \g_@@_one_op_fp { max(\g_benchmark_time_fp, 1e-8) }
384 \int_gset:Nn \g_@@_duration_int
385 { \fp_to_int:n { 65536 * \g_benchmark_duration_target_fp } }
386 }
387 % \end{macrocode}
388 % \end{macro}
389 %
390 % \begin{macro}{\@@_fp_to_tl:N, \@@_fp_to_tl_aux:nN}
391 % Similar to \cs{fp_to_tl:N} but rounds to $3$ significant digits and
392 % uses scientific notation starting from |1e3|.
393 % \begin{macrocode}
394 \cs_new:Npn \@@_fp_to_tl:N #1
395 {
396 \fp_compare:nTF { abs(#1) < 1000 }
397 { \fp_to_tl:n { round(#1, 2 - logb(#1)) } }
398 {
399 \exp_args:Nf \@@_fp_to_tl_aux:nN
400 { \fp_to_int:n { logb(#1) } } #1
401 }
402 }
403 \cs_new:Npn \@@_fp_to_tl_aux:nN #1#2
404 { \fp_to_tl:n { round(#2 * 1e-#1, 2) } e#1 }
405 % \end{macrocode}
406 % \end{macro}
407 %
408 % \begin{macro}{\@@_display:}
409 % Function to display the time that was measured and the estimated
410 % number of operations.
411 % \begin{macrocode}
412 \cs_new_protected:Npn \@@_display:
413 {
414 \iow_term:e
415 {
416 \@@_fp_to_tl:N \g_benchmark_time_fp \c_space_tl seconds \c_space_tl
417 ( \@@_fp_to_tl:N \g_benchmark_ops_fp \c_space_tl ops)
418 }
419 }
420 % \end{macrocode}
421 % \end{macro}
422 %
423 % \subsection{Benchmark tic toc}
424 %
425 % \begin{variable}{\g_@@_tictoc_int, \g_@@_tictoc_seq, \l_@@_tictoc_pop_tl}
426 % \begin{macrocode}
427 \int_new:N \g_@@_tictoc_int
428 \seq_new:N \g_@@_tictoc_seq
429 \tl_new:N \l_@@_tictoc_pop_tl
430 % \end{macrocode}
431 % \end{variable}
432 %
433 % \begin{macro}[EXP]{\@@_tictoc_prefix:}
434 % We include the package name in analogy with continuation lines of
435 % error/warning messages.
436 % \begin{macrocode}
437 \cs_new:Npn \@@_tictoc_prefix:
438 {
439 (l3benchmark) \c_space_tl
440 + \prg_replicate:nn { \g_@@_tictoc_int } { -+ } \c_space_tl
441 }
442 % \end{macrocode}
443 % \end{macro}
444 %
445 % \begin{macro}{\benchmark_tic:}
446 % \begin{macrocode}
447 \cs_new_protected:Npn \benchmark_tic:
448 {
449 \iow_term:e { \@@_tictoc_prefix: TIC }
450 \exp_args:NNf \seq_gput_right:Nn \g_@@_tictoc_seq { \sys_timer: }
451 \int_gincr:N \g_@@_tictoc_int
452 }
453 % \end{macrocode}
454 % \end{macro}
455 %
456 % \begin{macro}{\benchmark_toc:, \@@_toc:}
457 % \begin{macrocode}
458 \cs_new:Npn \benchmark_toc:
459 {
460 \seq_gpop_right:NNTF \g_@@_tictoc_seq \l_@@_tictoc_pop_tl
461 { \@@_toc: }
462 { \msg_error:nn { benchmark } { toc-first } }
463 }
464 \cs_new_protected:Npn \@@_toc:
465 {
466 \int_gdecr:N \g_@@_tictoc_int
467 \fp_gset:Nn \g_benchmark_time_fp
468 { ( \sys_timer: - \l_@@_tictoc_pop_tl ) / 65536 }
469 \iow_term:e
470 {
471 \@@_tictoc_prefix:
472 TOC: \c_space_tl
473 \@@_fp_to_tl:N \g_benchmark_time_fp \c_space_tl s
474 }
475 }
476 \msg_new:nnn { benchmark } { toc-first }
477 {
478 \token_to_str:N \benchmark_toc: \c_space_tl without~
479 \token_to_str:N \benchmark_tic: \c_space_tl !
480 }
481 % \end{macrocode}
482 % \end{macro}
483 %
484 % \begin{macrocode}
485 %</code>
486 % \end{macrocode}
487 %
488 % \end{implementation}
489 %
490 % \PrintIndex