% luatex
% tester si Replicate est plus ou moins rapide avec \romannumeral
% % jfbu, Vendredi 06 août 2021

% Dimanche 8 août:
% - je supprime le \input shared_batteries.tex et met le code directement dans
% le fichier

% - j'élimine les deux valeurs maximales et minimales de chaque run

% - je passe à 987 x 12345 à la place de 1000 x 12345 et
%   en particulier je n'utilise plus le \ReplicateM optimisé,
%   seulement le \Replicate{N}{tokens} general

% Conclusion: ??? seems to depend on alignment of the stars
%
% With TL2018 (luatex 1.07.0, files on a hard disk),
% and TEXMFCNF not set, clearly 2% loss if using \romannumeral
% with TEXMFCNF set it seems small (<1%) gain if using \romannumeral... 

% With TL2021 luatex 1.13.0, files on a SSD, I now (late afternoon)
% observe the same, typically
% Total avec: 1.64137pt (average 0.27356pt)
% Total sans: 1.65717pt (average 0.2762pt)
% with export TEXMFCNF="$(pwd):"
%
% whereas, typically with TEXMFCNF unset
%
% Total avec: 1.6925pt (average 0.28209pt)
% Total sans: 1.64622pt (average 0.27437pt)
%
% (meaning I can execute 12 times in a row the file with luatex and confirm
%  above typical behaviour)
%
% But earlier today I did not have this at all...

% specialized variant optimized for replicating exactly 1000 times
% \long\def\ReplicateM#1{\ReplicateC{#1#1#1#1#1#1#1#1#1#1}}%
% \long\def\ReplicateC#1{\ReplicateX{#1#1#1#1#1#1#1#1#1#1}}%
\long\def\ReplicateX#1{#1#1#1#1#1#1#1#1#1#1}%

% tester avec luatex car c'est pour drag-race
\ifx\directlua\undefined\else
% https://tex.stackexchange.com/a/32531
\directlua{pdfelapsedtimer_basetime=0}%
\def\pdfresettimer{\directlua{pdfelapsedtimer_basetime = os.clock()}}%
\def\pdfelapsedtime{\numexpr\directlua{tex.print(math.floor((os.clock()-pdfelapsedtimer_basetime)*65536+0.5))}\relax}%
\fi  

\catcode`@ 11
\catcode`_ 11

% 8 août, exclusion des valeurs extrêmes
\newdimen\Tall
\newdimen\Tmin      \Tmin\maxdimen    
\newdimen\Tmax                        
\newdimen\Tnextmin  \Tnextmin\maxdimen
\newdimen\Tnextmax

\newdimen\Uall
\newdimen\Umin     \Umin\maxdimen
\newdimen\Umax
\newdimen\Unextmin \Unextmin\maxdimen
\newdimen\Unextmax

% \ReplicateX{%
% \pdfresettimer
% \ReplicateM{\Replicate{12345}{\relax}}%
% \edef\T{\the\dimexpr\pdfelapsedtime sp}%

% \immediate\write128{avec: \T}%
% \advance\Tall\T
% }%

% let's try first with the "no \romannumeral" \Replicate

\def\Replicate#1{\expandafter\Rep_#1\endcsname}%
\def\Rep_ #1{\csname Repf#1\Repa}%
\def\Repa #1{\csname Rep#1\Repa}%
\def\Rep\Repa{\endcsname}%
\long\expandafter\def\csname Rep0\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}}%
\long\expandafter\def\csname Rep1\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1}%
\long\expandafter\def\csname Rep2\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1}%
\long\expandafter\def\csname Rep3\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1}%
\long\expandafter\def\csname Rep4\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1}%
\long\expandafter\def\csname Rep5\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1}%
\long\expandafter\def\csname Rep6\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1#1}%
\long\expandafter\def\csname Rep7\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1#1#1}%
\long\expandafter\def\csname Rep8\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1#1#1#1}%
\long\expandafter\def\csname Rep9\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1#1#1#1#1}%
\long\expandafter\def\csname Repf0\endcsname #1{}%
\long\expandafter\def\csname Repf1\endcsname #1{#1}%
\long\expandafter\def\csname Repf2\endcsname #1{#1#1}%
\long\expandafter\def\csname Repf3\endcsname #1{#1#1#1}%
\long\expandafter\def\csname Repf4\endcsname #1{#1#1#1#1}%
\long\expandafter\def\csname Repf5\endcsname #1{#1#1#1#1#1}%
\long\expandafter\def\csname Repf6\endcsname #1{#1#1#1#1#1#1}%
\long\expandafter\def\csname Repf7\endcsname #1{#1#1#1#1#1#1#1}%
\long\expandafter\def\csname Repf8\endcsname #1{#1#1#1#1#1#1#1#1}%
\long\expandafter\def\csname Repf9\endcsname #1{#1#1#1#1#1#1#1#1#1}%

\ReplicateX{%
\pdfresettimer
\Replicate{987}{\Replicate{12345}{\relax}}%
\edef\U{\the\dimexpr\pdfelapsedtime sp}%

\immediate\write128{sans: \U}%
\advance\Uall\U
%
\ifdim\U>\Umax
   \Unextmax=\Umax
   \Umax\U
\else\ifdim\U>\Unextmax
   \Unextmax=\U
\fi\fi
%
\ifdim\U<\Umin
   \Unextmin=\Umin
   \Umin\U
\else\ifdim\U<\Unextmin
   \Unextmin=\U
\fi\fi
}%

% \Replicate with \romannumeral as in shared_batteries.tex
\def\Replicate#1{\romannumeral\expandafter\Rep_#1\endcsname}%
\def\Rep_ #1{\csname Repf#1\Repa}%
\def\Repa #1{\csname Rep#1\Repa}%
\def\Rep\Repa{\endcsname}%
\long\expandafter\def\csname Rep0\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}}%
\long\expandafter\def\csname Rep1\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1}%
\long\expandafter\def\csname Rep2\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1}%
\long\expandafter\def\csname Rep3\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1}%
\long\expandafter\def\csname Rep4\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1}%
\long\expandafter\def\csname Rep5\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1}%
\long\expandafter\def\csname Rep6\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1#1}%
\long\expandafter\def\csname Rep7\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1#1#1}%
\long\expandafter\def\csname Rep8\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1#1#1#1}%
\long\expandafter\def\csname Rep9\endcsname #1%
    {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1#1#1#1#1}%
\long\expandafter\def\csname Repf0\endcsname #1{\z@}%
\long\expandafter\def\csname Repf1\endcsname #1{\z@ #1}%
\long\expandafter\def\csname Repf2\endcsname #1{\z@ #1#1}%
\long\expandafter\def\csname Repf3\endcsname #1{\z@ #1#1#1}%
\long\expandafter\def\csname Repf4\endcsname #1{\z@ #1#1#1#1}%
\long\expandafter\def\csname Repf5\endcsname #1{\z@ #1#1#1#1#1}%
\long\expandafter\def\csname Repf6\endcsname #1{\z@ #1#1#1#1#1#1}%
\long\expandafter\def\csname Repf7\endcsname #1{\z@ #1#1#1#1#1#1#1}%
\long\expandafter\def\csname Repf8\endcsname #1{\z@ #1#1#1#1#1#1#1#1}%
\long\expandafter\def\csname Repf9\endcsname #1{\z@ #1#1#1#1#1#1#1#1#1}%


\ReplicateX{%
\pdfresettimer
\Replicate{987}{\Replicate{12345}{\relax}}%
\edef\T{\the\dimexpr\pdfelapsedtime sp}%

\immediate\write128{avec: \T}%
\advance\Tall\T
%
\ifdim\T>\Tmax
   \Tnextmax=\Tmax
   \Tmax=\T
\else\ifdim\T>\Tnextmax
   \Tnextmax=\T
\fi\fi
%
\ifdim\T<\Tmin
   \Tnextmin=\Tmin
   \Tmin=\T
\else\ifdim\T<\Tnextmin
   \Tnextmin=\T
\fi\fi
}%

\advance\Tall-\Tmax
\advance\Tall-\Tnextmax
\advance\Tall-\Tmin
\advance\Tall-\Tnextmin

\advance\Uall-\Umax
\advance\Uall-\Unextmax
\advance\Uall-\Umin
\advance\Uall-\Unextmin

\immediate\write128{}
\immediate\write128{Total avec: \the\Tall\space (average \the\dimexpr\Tall/6)}
\immediate\write128{Total sans: \the\Uall\space (average \the\dimexpr\Uall/6)}

\bye
