# Why is LogLogPlot[] slower than Plot[]?

I notice that LogLogPlot runs quite bit more slowly than ordinary Plot. For example, consider the following two blocks of code which plots (using Manipulate) a region in linear scale and log-linear scale. Notice how the log-log-scale manipulation runs more sluggishly. Is there a way to speed up the log-log-scale version?

Linear-scale code:

 Mhad = 0.93827;
Table[Tooltip[(MxSq - M^2) x/(1 - x) /. M -> Mhad, "x=" <> ToString[x]], {x, 0, .9, .1}];
Manipulate[Plot[{1/s (s - M^2) (s - MxSq) /. M -> Mhad, %},
{MxSq, Mhad^2, 100}, PlotRange -> {{0, 100}, {0, 100}},
AspectRatio -> 1, Filling -> {1 -> Axis}, Frame -> True,
PlotStyle -> Table[If[i == 1,
RGBColor[0, 0, 0],
{RGBColor[1, 0, 0], Thickness[.001]}], {i, 1, 11}]], {{s, 50}, 1,
400}]


Log-log-scale:

 Table[Tooltip[(MxSq - M^2) x/(1 - x) /. M -> Mhad, "x=" <> ToString[x]], {x, 0, .9, .1}];
Manipulate[LogLogPlot[{1/s (s - M^2) (s - MxSq) /. M -> Mhad, %},
{MxSq, Mhad^2, 100}, PlotRange -> {{.8, 100}, {.8, 100}},
AspectRatio -> 1, Filling -> {1 -> Axis}, Frame -> True,
PlotStyle -> Table[If[i == 1,
RGBColor[0, 0, 0],
{RGBColor[1, 0, 0], Thickness[.001]}], {i, 1, 11}]], {{s, 50}, 1,
400}]

• I cannot replicate the timing difference for this example. On a clean kernel, the log-log case is if anything faster according to AbsoluteTiming. But I replaced the % with an explicit label for the Table in the previous line - perhaps that is the issue? – Verbeia Sep 8 '12 at 8:37

Using

Mhad = 0.93827;
t = Table[
Tooltip[(MxSq - M^2) x/(1 - x) /. M -> Mhad,"x=" <> ToString[x]], {x, 0, .9, .1}];
With[{s = 50},
p1=Plot[{1/s (s - M^2) (s - MxSq) /. M -> Mhad, t}, {MxSq, Mhad^2,100},
PlotRange -> {{.8, 100}, {.8, 100}}, AspectRatio -> 1,
Filling -> {1 -> Axis}, Frame -> True,
PlotStyle ->
Table[If[i == 1, RGBColor[0, 0, 0],
{RGBColor[1, 0, 0], Thickness[.001]}], {i, 1,11}]]
] // AbsoluteTiming


and a similar one for LogLogPlot I find that Plot is about 2.7 times faster than LogLogPlot.

This is hardly surprising as the LogLogPlot is much more complex than the Plot. You can use FullForm to examine the underlying graphics primitives. The bytecount of these primitives have the following ratio:

(p2 // FullForm // ByteCount)/ (p1 // FullForm // ByteCount)


2.604100491

or take for instance the number of List instructions (all coordinates are in lists):

Cases[p1, List[___], Infinity] // Length


687

Cases[p2, List[___], Infinity] // Length


1202

It simply takes more coordinates to describe a curve accurately than for a set of straight lines.

One way to speed up the Manipulate may be the use of ControlActive, where you draw graphics with lower quality as long as the controls are being manipulated and a final one with higher quality if user-interaction stops.

Manipulate[
ControlActive[
LogLogPlot[{1/s (s - M^2) (s - MxSq) /. M -> Mhad}, {MxSq, Mhad^2,100},
PlotRange -> {{.8, 100}, {.8, 100}}, AspectRatio -> 1, Frame -> True,
PlotPoints -> 10, MaxRecursion -> 1],
LogLogPlot[{1/s (s - M^2) (s - MxSq) /. M -> Mhad, t}, {MxSq, Mhad^2, 100},
PlotRange -> {{.8, 100}, {.8, 100}},
AspectRatio -> 1, Filling -> {1 -> Axis}, Frame -> True,
PlotStyle ->
Table[If[i == 1, RGBColor[0, 0, 0],
{RGBColor[1, 0, 0], Thickness[.001]}], {i, 1, 11}]]
],
{{s, 50}, 1, 400}
]


which switches between and I changed PlotPoints, MaxRecursion, Filling and the plotting of the background curves. Adapt to your own needs.

• I don't think the number of points plotted is what matters here... For instance, if you sow the points and then use listplot to plot the same points, it is much faster. Rather, I think it has to do with determining which points to plot to get the best result. It might be the internal optimization routines that take longer – rm -rf Sep 8 '12 at 15:41
• @r.m. That's probably true. Note that I used the amount of primitives as a measure of the complexity of the plot and left it as an open question whether their production or their usage to show the plot in the FE is the cause of the slowness. – Sjoerd C. de Vries Sep 8 '12 at 18:18