Does the use of macro in LaTeX slow down the compilation process?
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 :-)