[latex3-commits] [git/LaTeX3-latex3-latex3] master: Remove l3benchmark ..._apply:nN in favor of ..._silent:n and public variable (a1c1090)

Bruno Le Floch bruno at le-floch.fr
Wed May 9 00:28:40 CEST 2018


Repository : https://github.com/latex3/latex3
On branch  : master
Link       : https://github.com/latex3/latex3/commit/a1c1090af1dc34347bc72b54462446fa71908a39

>---------------------------------------------------------------

commit a1c1090af1dc34347bc72b54462446fa71908a39
Author: Bruno Le Floch <bruno at le-floch.fr>
Date:   Tue May 8 18:28:40 2018 -0400

    Remove l3benchmark ..._apply:nN in favor of ..._silent:n and public variable
    
    Experience (in the test file) suggests that this would be much more convenient.


>---------------------------------------------------------------

a1c1090af1dc34347bc72b54462446fa71908a39
 l3trial/l3benchmark/l3benchmark.dtx              |   95 ++++++++++++----------
 l3trial/l3benchmark/testfiles/m3benchmark001.lvt |   23 ++----
 2 files changed, 61 insertions(+), 57 deletions(-)

diff --git a/l3trial/l3benchmark/l3benchmark.dtx b/l3trial/l3benchmark/l3benchmark.dtx
index ebd9bfa..7397173 100644
--- a/l3trial/l3benchmark/l3benchmark.dtx
+++ b/l3trial/l3benchmark/l3benchmark.dtx
@@ -86,42 +86,44 @@
 %
 % \section{Benchmark}
 %
-% \begin{function}{\benchmark_display:n}
+% \begin{variable}{\g_benchmark_time_fp}
+%   Functions such as \cs{benchmark:n} store the measured time in
+%   \cs{g_benchmark_time_fp} (in seconds).
+% \end{variable}
+%
+% \begin{function}{\benchmark_display:}
 %   \begin{syntax}
-%     \cs{benchmark_display:n} \Arg{time}
+%     \cs{benchmark_display:}
 %   \end{syntax}
-%   Prints the \meta{time} (in seconds) to the terminal.  This function
-%   can be redefined by the user.
+%   Prints the time \cs{g_benchmark_time_fp} (in seconds) to the
+%   terminal.  This function is called by functions such as
+%   \cs{benchmark:n} and can be redefined by the user.
 % \end{function}
 %
-% \begin{function}{\benchmark_once:n, \benchmark_once_apply:nN}
+% \begin{function}{\benchmark_once:n, \benchmark_once_silent:n}
 %   \begin{syntax}
 %     \cs{benchmark_once:n} \Arg{code}
-%     \cs{benchmark_once_apply:nN} \Arg{code} \Arg{function}
 %   \end{syntax}
-%   Measures the time taken by \TeX{} to run the \meta{code} once, and
-%   calls \cs{benchmark_display:n} \Arg{time} with the \meta{time} in
-%   seconds.  The code is run only once so the time may be quite
-%   inaccurate for fast code.  The \cs{benchmark_once_apply:nN} function
-%   calls \meta{function} \Arg{time} instead of \cs{benchmark_display:n}
-%   \Arg{time}.
+%   Sets \cs{g_benchmark_time_fp} to the time taken by \TeX{} to run the
+%   \meta{code} once, and calls \cs{benchmark_display:}.  The code is
+%   run only once so the time may be quite inaccurate for fast code.
+%   The \cs{benchmark_once_silent:n} function omits the call to
+%   \cs{benchmark_display:}.
 % \end{function}
 %
-% \begin{function}{\benchmark:n, \benchmark_apply:nN}
+% \begin{function}{\benchmark:n, \benchmark_silent:n}
 %   \begin{syntax}
 %     \cs{benchmark:n} \Arg{code}
-%     \cs{benchmark_apply:nN} \Arg{code} \meta{function}
 %   \end{syntax}
-%   Measures the time taken by \TeX{} to run the \meta{code}, and calls
-%   \cs{benchmark_display:n} \Arg{time} with the \meta{time} in seconds.
-%   This function cannot be nested.  The \meta{code} may be run many
-%   times in a row, and not within a group, thus it should not have
-%   side-effects.  The \cs{benchmark_apply:nN} function calls
-%   \meta{function} \Arg{time} instead of \cs{benchmark_display:n}
-%   \Arg{time}.
+%   Sets \cs{g_benchmark_time_fp} to the time taken by \TeX{} to run the
+%   \meta{code}, and calls \cs{benchmark_display:}.  This function
+%   cannot be nested.  The \meta{code} may be run many times in a row,
+%   and not within a group, thus it should not have side-effects.  The
+%   \cs{benchmark_silent:n} function omits the call to
+%   \cs{benchmark_display:}.
 % \end{function}
 %
-% \begin{function}{\benchmark_tic:,\benchmark_toc:}
+% \begin{function}{\benchmark_tic:, \benchmark_toc:}
 %   \begin{syntax}
 %     \cs{benchmark_tic:} \meta{slow code} \cs{benchmark_toc:}
 %   \end{syntax}
@@ -312,6 +314,23 @@ end
 %<@@=benchmark>
 %    \end{macrocode}
 %
+% \begin{variable}{\g_benchmark_time_fp}
+%   Functions such as \cs{benchmark:n} store the measured time in
+%   \cs{g_benchmark_time_fp} (in seconds).
+%    \begin{macrocode}
+\fp_new:N \g_benchmark_time_fp
+%    \end{macrocode}
+% \end{variable}
+%
+% \begin{macro}{\benchmark_display:}
+%   Function to display the time that was measured.  This can be
+%   redefined by the user.
+%    \begin{macrocode}
+\cs_new_protected:Npn \benchmark_display:
+  { \iow_term:x { \fp_to_tl:N \g_benchmark_time_fp ~ seconds } }
+%    \end{macrocode}
+% \end{macro}
+%
 %    \begin{macrocode}
 \sys_if_timer_exist:F
   {
@@ -325,12 +344,13 @@ end
       }
     \cs_new_protected:Npn \benchmark_once:n #1
       { \msg_error:nn { benchmark } { no-time } }
-    \cs_new_protected:Npn \benchmark_once_apply:nN #1#2
+    \cs_new_protected:Npn \benchmark_once_silent:n #1
       { \msg_error:nn { benchmark } { no-time } }
     \cs_new_protected:Npn \benchmark:n #1
       { \msg_error:nn { benchmark } { no-time } }
-    \cs_new_protected:Npn \benchmark_apply:nN #1#2
+    \cs_new_protected:Npn \benchmark_silent:n #1
       { \msg_error:nn { benchmark } { no-time } }
+    \fp_gset:Nn \g_benchmark_time_fp { nan }
     \msg_critical:nn { benchmark } { no-time }
   }
 %    \end{macrocode}
@@ -402,15 +422,15 @@ end
 % \end{macro}
 % \end{variable}
 %
-% \begin{macro}{\benchmark_once:n, \benchmark_once_apply:nN}
+% \begin{macro}{\benchmark_once:n, \benchmark_once_silent:n}
 %   Convert from scaled seconds to seconds.
 %    \begin{macrocode}
 \cs_new_protected:Npn \benchmark_once:n #1
-  { \benchmark_once_apply:nN {#1} \benchmark_display:n }
-\cs_new_protected:Npn \benchmark_once_apply:nN #1#2
+  { \benchmark_once_silent:n {#1} \benchmark_display: }
+\cs_new_protected:Npn \benchmark_once_silent:n #1
   {
     \@@_raw:nN {#1} \l_@@_time_int
-    \exp_args:Nf #2 { \fp_to_tl:n { \l_@@_time_int / 65536 } }
+    \fp_gset:Nn \g_benchmark_time_fp { \l_@@_time_int / 65536 }
   }
 %    \end{macrocode}
 % \end{macro}
@@ -479,7 +499,7 @@ end
 %    \end{macrocode}
 % \end{macro}
 %
-% \begin{macro}{\benchmark:n, \benchmark_apply:nN}
+% \begin{macro}{\benchmark:n, \benchmark_silent:n}
 %   The main timing function. First time the user code once.  If that
 %   took more than a third of a second we're done.  If that took much
 %   less than a second, quadruple the number of copies until it takes a
@@ -494,8 +514,8 @@ end
 \tl_new:N \g_@@_code_tl
 \fp_new:N \l_@@_time_fp
 \cs_new_protected:Npn \benchmark:n #1
-  { \benchmark_apply:nN {#1} \benchmark_display:n }
-\cs_new_protected:Npn \benchmark_apply:nN #1#2
+  { \benchmark_silent:n {#1} \benchmark_display: }
+\cs_new_protected:Npn \benchmark_silent:n #1
   {
     \int_set:Nn \l_@@_repeat_int { 1 }
     \tl_gset:Nn \g_@@_code_tl {#1}
@@ -525,25 +545,14 @@ end
     \@@_run:N \l_@@_time_d_int
     \@@_set_median:
     \prg_break_point:
-    \fp_set:Nn \l_@@_time_fp
+    \fp_gset:Nn \g_benchmark_time_fp
       { \l_@@_time_median_int / \l_@@_repeat_int / 65536 }
-    \exp_args:Nf #2 { \fp_to_tl:N \l_@@_time_fp }
   }
 \cs_new_protected:Npn \@@_run:N
   { \exp_args:NNo \@@_raw_replicate:nnN \l_@@_repeat_int { \g_@@_code_tl } }
 %    \end{macrocode}
 % \end{macro}
 %
-% \begin{macro}{\benchmark_display:n}
-%   Function to display the time that was measured.
-%   This can be redefined by the user.
-%    \begin{macrocode}
-\cs_new_protected:Npn \benchmark_display:n #1
-  { \iow_term:x { \tl_to_str:n {#1} ~ seconds } }
-\cs_generate_variant:Nn \benchmark_display:n { f }
-%    \end{macrocode}
-% \end{macro}
-%
 % \subsection{Benchmark tic toc}
 %
 % \begin{variable}{\g_@@_tictoc_int, \g_@@_tictoc_seq}
diff --git a/l3trial/l3benchmark/testfiles/m3benchmark001.lvt b/l3trial/l3benchmark/testfiles/m3benchmark001.lvt
index c82ad2b..c51e064 100644
--- a/l3trial/l3benchmark/testfiles/m3benchmark001.lvt
+++ b/l3trial/l3benchmark/testfiles/m3benchmark001.lvt
@@ -17,9 +17,9 @@
 \sys_if_timer_exist:F
   {
     \benchmark_once:n \ERROR \TRUE
-    \benchmark_once_apply:nN \ERROR \ERROR \TRUE
+    \benchmark_once_silent:n \ERROR \TRUE
     \benchmark:n \ERROR \TRUE
-    \benchmark_apply:nN \ERROR \ERROR \TRUE
+    \benchmark_silent:n \ERROR \TRUE
     \END
   }
 
@@ -27,10 +27,6 @@
   { \int_set:Nn \l__benchmark_duration_int { 65536 / 10 } }
 
 \OMIT
-\cs_new:Npn \test:nn #1#2 { #2 {#1} }
-\TIMO
-
-\OMIT
 \exp_args:NNx \seq_set_from_clist:Nn \l_tmpa_seq
   { \prg_replicate:nn { 123 } { a , } }
 \cs_new_protected:Npn \test_slow_code:
@@ -40,12 +36,11 @@
 \TEST { benchmark_once }
   {
     \int_zero:N \l_tmpa_int
-    \benchmark_once_apply:nN { \int_incr:N \l_tmpa_int }
-      \test:nn { \fp_set:Nn \l_tmpa_fp }
+    \benchmark_once_silent:n { \int_incr:N \l_tmpa_int }
     \int_log:N \l_tmpa_int    % Check the code was done only once
-    \fp_compare:nTF { \l_tmpa_fp < 5e-5 }  % Check it took 0 to 3 scaled seconds
+    \fp_compare:nTF { \g_benchmark_time_fp < 5e-5 }  % Check it took 0 to 3 scaled seconds
       { \TRUE }
-      { \ERROR \fp_show:N \l_tmpa_fp }
+      { \ERROR \fp_show:N \g_benchmark_time_fp }
   }
 
 \TEST { benchmark }
@@ -54,10 +49,10 @@
     \clist_clear:N \l_tmpb_clist
     \prg_replicate:nn { 3 }
       {
-        \benchmark_apply:nN { \test_slow_code: }
-          \test:nn { \clist_put_right:Nn \l_tmpa_clist }
-        \benchmark_apply:nN { \test_slow_code: \test_slow_code: }
-          \test:nn { \clist_put_right:Nn \l_tmpb_clist }
+        \benchmark_silent:n { \test_slow_code: }
+        \clist_put_right:Nx \l_tmpa_clist { \fp_to_tl:N \g_benchmark_time_fp }
+        \benchmark_silent:n { \test_slow_code: \test_slow_code: }
+        \clist_put_right:Nx \l_tmpb_clist { \fp_to_tl:N \g_benchmark_time_fp }
       }
     \fp_compare:nTF % Check that doing code twice takes between 1.5 and 2.5 times as much
       {





More information about the latex3-commits mailing list