2

I use a lot of unicode symbols, auto-inserted with a TeXStudio macro (e.g. "\alpha " --> "α"). This makes it easier for me to read my .tex file. These (~100) characters are declared in a .sty file called in the preamble, e.g.

\DeclareUnicodeCharacter{03B1}{\alpha}

Could this slow down compilation of the document?

5
  • (in pdfLaTeX) Yes, it does slow down the compilation because defining: \DeclareUnicodeCharacter{03B1}{\alpha} makes the character α be a command that expands to \alpha. However the overhead of expanding α to \alpha is insignificant in a real document, so if it makes typing easier, there's no real reason for you to don't use that. May 29, 2020 at 18:16
  • To be clear, you are saying the compilation time is (minimally) increased solely by the inclusion of \DeclareUnicodeCharacter{03B1}{\alpha} in the .sty file, and thus isn't increasing in the repeated use of α in the document? May 29, 2020 at 18:46
  • 3
    No, both increase the compilation time. Roughly speaking, \DeclareUnicodeCharacter{03B1}{\alpha} is like \newcommand{α}{\alpha}, then each use of α eventually becomes \alpha. But really, that should make no visible difference in the time it takes to build the document. Just to give you an idea, a document with 7000 α takes 0.118 s to run on my machine, whereas the same document with 7000 \alpha takes 0.112 s. May 29, 2020 at 18:56
  • 1
    @PhelypeOleinik how many cups of coffee can you make in that .006s though? May 29, 2020 at 19:08
  • 1
    @DavidCarlisle Not nearly enough :-) May 29, 2020 at 19:32

2 Answers 2

1

I made an experiment: the code

\documentclass{article}

\begin{document}

\ExplSyntaxOn
\prg_replicate:nn {100000} { $\alpha$$\alpha$$\alpha$$\alpha$$\alpha$$\alpha$$\alpha$$\alpha$$\alpha$$\alpha$\par }
\ExplSyntaxOff

\end{document}

run with time pdflatex -draftmode (that simply doesn't produce the PDF file) shows

real  0m1.491s
user  0m1.377s
sys   0m0.073s

If I change to

\documentclass{article}
\DeclareUnicodeCharacter{03B1}{\alpha}

\begin{document}

\ExplSyntaxOn
\prg_replicate:nn {100000} { $α$$α$$α$$α$$α$$α$$α$$α$$α$$α$\par }
\ExplSyntaxOff

\end{document}

the terminal shows

real  0m3.213s
user  0m3.070s
sys   0m0.090s

The run takes about twice as long. In a previous experiment, with just 100000 occurrences of alpha, the run took 20% longer.

I don't think there's anything to be worried about for a normal document.

If I repeat the same experiment with unicode-math and LuaLaTeX, the execution times are comparable (but much longer, of course).

7
  • I'm not really sure I fully understand the benchmark results. 100,000 times $\alpha$ (total 100k) takes half the time as 100,000 times 10 times $α$ (total 1M), meaning that $α$ is about five times faster than $\alpha$. Yet, 100,000 times $α$ (the 'previous experiment') is slower than 100,000 times $\alpha$. So where does this difference come from?
    – Marijn
    May 30, 2020 at 11:38
  • @Marijn It is not linear…
    – egreg
    May 30, 2020 at 12:37
  • Is there a reason you used $α$$α$$α$$α$$α$$α$$α$$α$$α$$α$ rather than simply $α$? May 30, 2020 at 13:33
  • @PatrickFerguson I did to produce just 100000 paragraphs, but with a total of a million instances.
    – egreg
    May 30, 2020 at 13:45
  • 1
    @egreg Why didn't you do the same for $\alpha$? May 30, 2020 at 13:49
0

If you’re using LuaLaTeX or XeLaTeX with the Unicode encoding, no. They use Unicode natively, and passing the code point for α over to the font renderer is actually faster than looking up the macro \alpha, detecting the current font, and converting that to the same codepoint.

In an 8-bit TeX engine, it’s the other way around: inputenc will set the Unicode character active and look up what command to insert into the document, so it would have been slightly faster to just enter the command.

However, the time of a human being reading your source is much more valuable than the microseconds that takes the computer.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .