This question already has an answer here:

Consider this TeX/LaTeX StackExchange question I posted yesterday. I asked for a way to create a particular macro with a somewhat idiosyncratic calling pattern, which is not trivial to code. After waking up and coming back to the site, I found that seemingly viable solutions had been suggested by users A.Ellett, Sean Allred, egreg and Andrew Stacey (links go to the answers).

Now it is worth noting that the efficiency (however you define this) of a particular approach to the problem is not the only thing that is of importance in deciding which solution to use. There are other things that might be considered: how versatile an approach is, how fragile/easy to break with unforeseen use-cases it is (I am using "fragile" in the generic sense, not in the TeX macro sense), how helpful its errors are when it is used incorrectly, how easily deciphered (and potentially repurposed) the TeX code is. For example, A.Ellett notes that their answer "is not fail-safe!"; I'm not sure what he means, but I assume he is warning that his version of the macro might break if used in the wrong way. For another example, egreg's approach mandates that one of the arguments must be in a TeX group ({...}), which is not a problem as such but is a potential "gotcha" given that a typical LaTeX command does not mandate this.

But it would still be interesting to know which approach performs best (in that it allows a document that uses the macro many times to be compiled fastest). In the Python programming language, a utility is provided that allows one to time alternative approaches to a problem to see which is most efficient, by running through each approach a certain number of times and seeing which one gives the least total time. In LaTeX, presumably the natural way to do it would be to create a document that does the same sort of thing - repeating each solution a large number of times - and which contains in the finished document a summary of the total time taken by each approach.

Is there a way of doing this? It could look something like:


\usepackage{tikz, xparse}


\foreach \file in {file1.tex, file2.tex, file3.tex}{
    \input{\file} % file should use \def to define \mymacro
    % Here we start a timer
    \foreach \x in {1, ..., 1000}{
    % Here we stop the timer and place the time taken in the macro \executiontime.
    The time taken for the approach in \file was: \executiontime.


NB. This is something approaching pseudocode, and won't compile if simply copy-pasted into a file!

marked as duplicate by egreg macros Sep 6 '14 at 22:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    You could look at this answer to see one way of repeating a block of code (a single assignment in that case) sufficient number of times that timing from the operating system makes sense. – David Carlisle Sep 2 '13 at 16:38
  • Is this of interest? tex.stackexchange.com/questions/204/… – Torbjørn T. Sep 2 '13 at 16:51
  • 3
    Here's my rule of thumb: If the answer is provided by a name like egreg, Carlisle, or Medina, it is fast...if it is provided by a name like Segletes, it may be slow, sometimes, quite slow. – Steven B. Segletes Sep 2 '13 at 16:52

Browse other questions tagged or ask your own question.