# Does the use of macro in LaTeX slow down the compilation process?

Use of macro eases document preparation, further modification, uniformity maintenance, just to name a few of the enormous benefits we enjoy.

But does the use of a macro slow down the compilation process?

How does LaTeX handle the macros? In place replacements?

-
Usually no, but it depends on what the macro is doing, and other stuff – daleif Mar 14 at 16:10
Macros that do writing stuff to files are slowing the down the process, but this depends on the amount of content to be written of course. (And this is basically not the fault of TeX macros but rather an OS issue then) – Christian Hupfer Mar 14 at 16:12
It naturally depends on the macro. – Ulrike Fischer Mar 14 at 16:30
The whole of LaTeX is macros. Only TeX has primitives. – jfbu Mar 14 at 17:39
@Masroor imagine a macro that never ends. most macros that you will use are simple substitution macros there you will not see any difference when compiling. – daleif Mar 14 at 18:24

Try the following input file expansion.tex:

\def\1{\empty\empty\empty\empty\empty\empty\empty\empty\empty\empty}
\def\2{\1\1\1\1\1\1\1\1\1\1}
\def\3{\2\2\2\2\2\2\2\2\2\2}
\def\4{\3\3\3\3\3\3\3\3\3\3}
\def\5{\4\4\4\4\4\4\4\4\4\4}
\def\6{\5\5\5\5\5\5\5\5\5\5}
\def\7{\6\6\6\6\6\6\6\6\6\6}
\def\8{\7\7\7\7\7\7\7\7\7\7}
\def\9{\8\8\8\8\8\8\8\8\8\8}
\def\0{\9\9\9\9\9\9\9\9\9\9}

\do

\bye


and time the compilation with

time tex '\def\do{\7}\input expansion'


I get

real    0m0.532s
user    0m0.504s
sys     0m0.012s


You can easily figure out the number of macro expansions required. With \8 in place of \7 I get

real    0m4.340s
user    0m4.126s
sys     0m0.017s


Yes, it's of course an exponential growth; don't try with \0 ;-) With \6 I get

real    0m0.157s
user    0m0.142s
sys     0m0.012s


which shows that compiling a reasonable document is not slowed down by using macros.

Macros are, well, macros: when expansion of a macro is required, TeX does in place replacement of the macro with the tokens resulting from the replacement text currently stored in memory.

-

With ConTeXt, it is possible to get more fine-grained information about typesetting time. The macro is \testfeature

\testfeature{n}{....}


will execute the second argument n times and report the execution time on the terminal. Let's compare simple macro expansion:

\def\ABC{ABC}
\starttext
\testfeature{100000}{ABC}
\testfeature{100000}{\ABC}
\stoptext


This gives:

system          > starting feature test

\wait =
system          > 100000 feature tests done (0.060s)

\wait =
system          > starting feature test

\wait =
system          > 100000 feature tests done (0.066s)

\wait =


So, typesetting ABC 10^6 times took 0.060s while typesetting \ABC 10^6 times took 0.066s. So, the difference is 6ms for 10^6 or 6 nano seconds per expansion! I wouldn't worry about the speed of macro expansion :-)

-