0

I'm trying to speed up my pgfplots (2000 samples per curve). One thing that causes them to be slow is that (unless pgf has a built-in optimizer) pgf is computing certain values multiple times. To illustrate the problem, consider the following example where, I believe, pgf will compute the logarithm of #1 twice.

\pgfmathdeclarefunction{sincos}{1}{\pgfmathparse{
    sin(log(#1))+cos(log(#1))
}}

While log(#1) might be sufficiently fast, imagine it is a complex custom function I declared with \pgfmathdeclarefunction.

How would one change sincos so it computes the logarithm of x only once? Could one declare something like a local variable? Maybe with \pgfmathsetmacro?

The solution should take into account that pgfplots is switching to pgf's fpu implementation when evaluting functions.

  • \pgfmathdeclarefunction{sincos}{1}{\pgfmathsetmacro\thelog{log(#1)}\pgfmathparse{sin(\thelog)+cos(\thelog)}}. This is not necessary faster if Lua backend is enabled. – Symbol 1 May 19 '17 at 18:15
  • Do not forget that you can externalize a tikzpicture. This will save your time once and for all. – Symbol 1 May 19 '17 at 18:17
  • I have made use of \pgfmathsetmacro as suggested and I was able to accelerate the generation of a single diagramm from 11 seconds to 8 seconds. That's a whopping 27% improvement. I'm already externalizing the pictures, but I'm using the preview package to generate a single PDF with all plots in it. – Sven May 19 '17 at 18:47
  • It takes lualatex 8.7 seconds to generate a single diagram while pdflatex does it in 8.1 seconds. Do I need to manually enable the lua backend? – Sven May 19 '17 at 18:56
  • Update: I had pgfplots set to compat=1.8 which doesn't enable the lua backend by default. I changed that and I also tried \pgfkeys{/pgf/luamath=parser}, but lualatex is still lower than pdflatex by a considerable amount. – Sven May 19 '17 at 19:02

Your Answer

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

Browse other questions tagged or ask your own question.