22

I was thinking it'd be nice to know how much time each package I load adds on to the total time between clicking "compile" and seeing the PDF come up on my screen, so that I can figure out which ones I shouldn't load unless necessary. If there were a way of measuring the precise amount of time it takes to compile (i.e., have the computer measure it, not try to approximate it by hand with a stopwatch), I can just remove one package at a time and measure the differences. But I can't find a way of doing this; I hunted around in the "log" file and didn't see this info, and I couldn't find any packages that add this functionality.

Ultimately, I admit it's not that big of a deal - compiling usually only takes a second, maybe two - but now I'm curious how to do this, and it could be relevant in much larger documents (with longer compile times).

18

This is somewhat OS dependent. On Linux and OS X you can use the time command from the command line.

time pdflatex myfile.tex

returns: (e.g.)

real    0m1.976s
user    0m0.331s
sys     0m0.091s

There may be a similar command in Windows. Depending on your editing environment, you can probably modify the latex command from within the editor (temporarily) to run the time command when you compile, which would make checking the times for different package configurations easier.

  • Thanks for your answer! As I should have mentioned in my question, I'm using TeXnicCenter on Windows, and I'm afraid I don't have any experience running LaTeX (or anything else) from a command line. But I suppose this is good motivation to figure that out :) When you say "modify the latex command", how would I do that in something like TeXnicCenter? – Zev Chonoles Aug 30 '11 at 23:04
  • 2
    @Zev since Windows doesn't have an exact parallel to the unix time command, it's not so obvious how to do this in Windows. There are a bunch of suggestions here: How to measure execution time of command in Windows command line. I don't know how easy any of these would be to integrate with TeXnicCenter. – Alan Munn Aug 30 '11 at 23:18
  • Ah, thanks for the link - I suppose it might be more involved to do then. – Zev Chonoles Aug 31 '11 at 2:47
12

If you are using the pdftex engine, you can measure the time that each package takes to be loaded by adding the following near the start of your document.

\makeatletter
\newcommand{\typeoutthetime}
  {\typeout{\strip@pt\dimexpr\pdfelapsedtime sp\relax sec.}}
\edef\@popfilename
  {%
    \unexpanded\expandafter{\@popfilename}%
    \noexpand\typeoutthetime
  }
\makeatother

You could also hook similarly at the end of the document with Heiko Oberdiek's atveryend package: \AtVeryVeryEnd{\typeoutthetime}.

  • Where does it print the result? I saw from the the log file the package was executing the hook, but it didn't seem to output anything in the log. :/ – JoonasD6 Dec 15 '19 at 21:55
  • @JoonasD6 In the log file. – Bruno Le Floch Dec 18 '19 at 1:17
  • In my project, I'm getting this and no compilation time after that: Package atveryend Info: Empty hook AfterLastShipout' on input line 542. (/compile/output.aux)` Package atveryend Info: Empty hook AtVeryEndDocument' on input line 542.` Package atveryend Info: Empty hook AtEndAfterFileList' on input line 542.` But I just noticed in a new file that I do get: Package atveryend Info: Executing hook AtVeryVeryEnd' on input line 28. 0.31827sec.` Hmm. (I apologise for code formatting; spaces didn't seem to block it correctly.) – JoonasD6 Dec 19 '19 at 7:26
  • In what you copied there is a time: "0.31827sec". – Bruno Le Floch Dec 19 '19 at 13:50
  • Yes, but that only appeared in a new, clean test file. The first log entries (concerning the project I actually do need to time) which say "empty hook", didn't have any seconds after them. Wonder if some package conflict or something can result in the hook failing. – JoonasD6 Dec 19 '19 at 15:16
11

By default ConTeXt gives

system          | total runtime: 35.093

at the end of each compile. This is the cumulative for all the runs needed to resolve references, etc. For more detailed usage, there is a timing module, which gives a graphical output of the resources used per page.

\usemodule[timing]
\starttext
 ,,,
 ...
\page
\ShowUsage{}
\stoptext

The output is similar to the graphs shown in Chapter XIX of the Mk manual.

  • 6
    Is there anything that ConTeXt can't do? :-) – Alan Munn Aug 30 '11 at 21:08
  • It can't reach the mass for now (I'd like to know why). – ℝaphink Aug 30 '11 at 21:50
  • Thank you for your answer! I'm not sure that ConTeXt is right for me; I'm mainly using LaTeX to type my math homework, and I don't think I need anything fancy (or at least, the discussion here mainly went over my head or wasn't important to me). But it's good to know that this ability exists elsewhere. – Zev Chonoles Aug 30 '11 at 23:14
10

The regstats package with option "timer" gives the time needed for the specific (pdflatex) compilation run, using \pdfelapsedtime.

%% When compiling with lua(la)tex (and wanting to use option timer=true)
%% the following line must be uncommented (i.e. remove the "%% ").
%% \directlua{starttime = os.clock()}
\documentclass{article}
\usepackage[timer=true]{regstats}[2012/04/01]
\begin{document}
When option \verb|timer| (or \verb|timer=true|) is chosen, also the time
needed for the compilation run is given. The used \verb|\pdfelapsedtime|
is not available, when lua(la)tex is used instead of pdf(la)tex to compile
the document. In that case at the very beginning of your TeX file say
\verb|\directlua{starttime = os.clock()}|
(even before \verb|\documentclass|!), and the timer option can also be
used with lua(la)tex. When neither lua(la)tex nor pdf(la)tex is used
to compile the document, the timer(-option) does not work.

For the resulting message, please compile and have a look at the end
of the log-file.

Because the compilation time for this example is usually quite short,
option \texttt{timer} is not demonstrated very spectacular.
\end{document}
3

TeX, LaTeX and all its distributives offer (at least since version 3)
a compilation command -time-statistics.

(You can find a list of compilation options and commands,
by opening a command line and execute latex --help.)

Not sure though, wether (and how) one may invoke this compilation command directly from .tex files.
Compare for example \batchmode to invoke -interaction=batchmode.

Still hope this may be useful to someone. :)

Besides, here's what its execution looks like: enter image description here

  • 1
    This is probably only for MiKTeX. There's no such option in TeX Live binaries. – egreg Sep 11 '18 at 16:07
  • Ahhh that is too bad. (Just found it and seemed useful.) Do you think I should adjust the answer then? – C-Star-W-Star Sep 11 '18 at 16:08
  • +1 I just checked, on MikTeX the order is available, elsewhere, I don't know. – AndréC Sep 11 '18 at 19:51
  • @AndréC: Thanks for checking! Yes,it seems weird since it isn't clear wether this is a MikTex specific command, or Tex command.. – C-Star-W-Star Sep 13 '18 at 0:25
1

You need to download timeit from Windows 2003 Resource Kit. Then run in Windows command line:

timeit xelatex.exe -synctex=1 -interaction=nonstopmode C:\Users\USERNAME\test_directory\test.tex. 

It worked on Windows 10

0

latexmk has the option -time. It will output the time of each pass of the TeX-engine as well as the accumulated total time to generate all the documents. For example, the command

latexmk -pdf -silent -time file.tex

gives the output

Latexmk: Run number 1 of rule 'pdflatex'
This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
Latexmk: Run number 2 of rule 'pdflatex'
This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
'pdflatex  -interaction=batchmode -recorder  "file.tex"': time = 0.23
'pdflatex  -interaction=batchmode -recorder  "file.tex"': time = 0.26
Accumulated processing time = 0.57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.