As well as fp
and the pgf
maths parser, I think I would evaluate xfp
/LaTeX3 FPU here. With a simple test set up such as
\documentclass{article}
\usepackage{fp}
\usepackage{xfp}
\usepackage{tikz}
\usepackage{l3benchmark}
\ExplSyntaxOn
\cs_new_eq:NN \benchmark \benchmark:n
\ExplSyntaxOff
\FPmessagesfalse
\newsavebox{\testbox}
\begin{document}
\benchmark{\sbox{\testbox}{\FPupn\result{2 2 root 180 40 / pi * sin *}}}
\benchmark{\sbox{\testbox}{\fpeval{sqrt(2) * sind(40)}}}
\benchmark{\sbox{\testbox}{\pgfmathparse{sqrt(2) * sin(40)}}}
\end{document}
on my system I get
0.00556 seconds (2.03e4 ops)
5.06e-4 seconds (1.83e3 ops)
2.23e-4 seconds (819 ops)
Unsurprisingly, fp
is slowest as it works to a huge number of places. On the other hand, the pgf
unit is fastest but not by much over the LaTeX3 code. The pgf
code is not expandable, and uses dimens internally, trading accuracy for speed. The latter is perfectly reasonable, but of course may or may not be acceptable depending on use case.
(For the test, I've used the UPN part of fp
largely as it seems fairest: the other two options offer parsing of expressions ...)
For completeness, if you are using LuaTeX then you can do the same using Lua, which is very fast:
\benchmark{\sbox{\testbox}{\directlua{tex.print(math.sqrt(2) * math.sin(40))}}}
gives 5.1e-5 seconds (187 ops)
seconds on my test setup.
Worth noting of course is that the speed does depend on the exact operation: if I go for a simple sum, pgf
and the LaTeX3 FPU are comparable:
\documentclass{article}
\usepackage{fp}
\usepackage{xfp}
\usepackage{tikz}
\usepackage{l3benchmark}
\ExplSyntaxOn
\cs_new_eq:NN \benchmark \benchmark:n
\ExplSyntaxOff
\FPmessagesfalse
\newsavebox{\testbox}
\begin{document}
\benchmark{\sbox{\testbox}{\FPupn\result{1.234 5 * 9.10 6.78 / +}}}
\benchmark{\sbox{\testbox}{\fpeval{1.234 * 5 + 6.78 / 9.10}}}
\benchmark{\sbox{\testbox}{\pgfmathparse{1.234 * 5 + 6.78 / 9.10}}}
\end{document}
gives
0.00231 seconds (8.42e3 ops)
2.25e-4 seconds (837 ops)
2.63e-4 seconds (930 ops)
If you want simple dimension calculations, nothing is going to beat primitive, most obviously \dimexpr
. Something like
\the\dimexpr 1.2cm + 3.445cm\relax
is 'clocked' by the benchmark code at 2.64e-6 seconds (9.85 ops)
on my system: really, really fast.
l3benchmark
. – Phelype Oleinik Dec 6 '18 at 17:45