I have a long .tex
file and I want to know the compilation time: duration, begin - finish.
Can I add this information to the PDF or display it in .log
file?
There are quite a few options. Here are three:
l3benchmark
Add this to the very top of your document:
\RequirePackage{l3benchmark}
\ExplSyntaxOn
\AtEndDocument { \benchmark_toc: }
\benchmark_tic:
\ExplSyntaxOff
The \benchmark_tic:
instruction will start a timer, and \AtEndDocument
the corresponding \benchmark_toc:
will measure the time elapsed since the \benchmark_tic:
. This will usually be enough to measure the execution time of your document: it will only include an extra \ExplSyntaxOff
and exclude some of the \end{document}
code. If you want to be really precise, then you can load etoolbox
and replace that with (test document included):
\RequirePackage{etoolbox}
\RequirePackage{l3benchmark}
\ExplSyntaxOn
\AfterEndDocument { \benchmark_toc: }
\use:n
{
\ExplSyntaxOff
\benchmark_tic:
}
% Test document:
\documentclass{article}
\usepackage{lipsum}
\begin{document}
\lipsum[1-150]
\end{document}
The \benchmark_tic:
will start right before your document, and the \benchmark_toc:
will be (almost) the last thing TeX will do before exiting. However this time does not include the initialisation of the TeX engine (should be negligible), and the final processing for the inclusion of fonts (in pdfTeX and LuaTeX) or conversion from the DVI file to PDF (in XeTeX), which may take a little while, depending on the size of the document and the number of included font files.
Running that the terminal (and the .log
file) will contain a (l3benchmark) + TIC
at the beginning, and then a (l3benchmark) + TOC: 0.745 s
before the final messages from the TeX engine.
arara
to run the job and time itWith the same example document, using an arara
header and running it:
% arara: pdflatex
\documentclass{article}
\usepackage{lipsum}
\begin{document}
\lipsum[1-150]
\end{document}
the terminal will show:
phelype@oleinik ~/tex.sx> arara test.tex
__ _ _ __ __ _ _ __ __ _
/ _` | '__/ _` | '__/ _` |
| (_| | | | (_| | | | (_| |
\__,_|_| \__,_|_| \__,_|
Processing 'test.tex' (size: 60 KB, last modified: 08/26/2019
17:14:41), please wait.
(PDFLaTeX) PDFLaTeX engine .............................. SUCCESS
Total: 1.22 seconds
phelype@oleinik ~/tex.sx>
so you have to bear in mind that arara
adds some overhead time, but it shouldn't be significant in larger jobs. If you run arara
with -l
(--log
) it will create an arara.log
file, whose final line will be something like:
26 ago 2019 17:20:20.548 INFO - Total: 1.22 seconds
If you're on Linux, you can use the command line tool time
:
phelype@oleinik ~/tex.sx> time pdflatex test.tex
after the processing the terminal will look like this:
Transcript written on test.log.
real 0m1.009s
user 0m0.984s
sys 0m0.024s
indicating approximately 1 s of run time.
Other systems might have other tools to do the same task, e.g. measure-command
in Windows Powershell.
The same document consistently reported in my system (approximate times, but not much difference between one run and the next):
(l3benchmark) + TOC: 0.745 s
Total: 1.22 seconds
real 0m1.009s
which probably means that TeX took 0.745 s to process my document, another 0.264 s (1.009 – 0.745) for the inclusion of fonts and closing the PDF file, and arara
took 0.220 s on top of that to do its thing. So depending on what exactly you want to measure, one way or another will give a better result, but all should be approximately equal for large enough jobs.
Note that since l3benchmark
measures from within the TeX run, the time it will print correspond to one TeX run only. If your document needs multiple runs and/or external tools like BibTeX, MakeIndex, etc. then the time that arara
or time
or whatever tool will print will certainly be bigger than the time printed by l3benchmark
, because they add up the time to run all processes, rather than one single TeX run.
Again, depends what on what you want to measure: if you're interested in the performance of your TeX code, then go for l3benchmark
, but if you're interested in the time that it really takes to run the job, then use something else.
time
in Bash is a builtin (see help time
) and does not offer the same formatting options (-f
) as the command line tool(s) of the name. Either use command time -f "..." ...
(or conversely builtin time ...
) or it will only work for the most trivial case (like in your example).
Commented
May 18, 2020 at 15:21
Building on Phelype's answer under "Using some other tool":
On Windows you can use Measure-Command
in PowerShell to get results similar to time
in Linux, e.g:
measure-command {latexmk sample-document}
More details at this link: https://stackoverflow.com/questions/3513650/timing-a-commands-execution-in-powershell
I have a long document (~70 pages with a long bibliography and lots of fonts) that takes 1 min 43 sec for first compilation, but l3benchmark
only records 21 sec in the .log
file.
Note: measure-command
does not work with the continuous preview function of latexmk
.
l3benchmark
only measures one run of the document, whereas measure-command
(I assume) measures the time the process latexmk sample-document
takes to end, and since latexmk
runs LaTeX multiple times, that might explain the difference you see. Thanks for the edit, by the way :-)
Commented
May 18, 2020 at 17:13
arara
, it will tell you the time it took. Other than that you can use, say,l3bechmark
. Or some other tool: if you're using Linux, you can dotime pdflatex mydoc.tex
.l3benchmark
does that or can do that, the manual only talks about 'printing to the terminal' but possibly that also means the log file?)l3benchmark
prints to the terminal (and to the log, by consequence). It could be changed to print to the pdf, if needed.arara
has its own log file, if asked for.time
is a command line tool, so no log for that one. I posted an answer with my three suggestions.