# How to optimize ContourPlot3D evaluation?

I tried to plot a certain 3D sine function:

ContourPlot3D[(Sin[x^2] + 2) * (Sin[y^2] + 2) * (Sin[z^2] + 2)
, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}]


but ran into an issue similar to (but not the same as) my last post. This suggested that the plot requires more than 512 megabytes of RAM to generate (I'm using the online version, as opposed to the desktop application which can go as far as the system itself is capable of handling).

The problem is most likely due to the fact that the expression being plotted is evaluated more frequently where the plot is rapidly changing, making a function with more steep areas more intensive. An example showed a function with one contour being evaluated around 260,000 times. In my case, it may be more than that (as I was using the default 3 contours), possibly over a million evaluations since this function changes increasingly rapidly as we travel further from 0,0,0.

How can I trade detail for lower memory consumption? Simply setting a lower value of PlotPoints does not fix the problem.

• You have a few questions with high up vote answers but have not accepted any (by clicking the checkmark under the voting buttons). Please revisit these questions and accept an appropriate answer where possible. Apr 4, 2022 at 0:04

You may use the MaxRecursion or PerformanceGoal options to trade detail for memory consumption.

Without MaxRecursion or PerformanceGoal

MaxMemoryUsed[
p = ContourPlot3D[(Sin[x^2] + 2)*(Sin[y^2] + 2)*(Sin[z^2] + 2)
, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}]
] // N@UnitConvert[Quantity[#, "Bytes"], "Megabytes"] &
p

Quantity[798.611, "Megabytes"] With MaxRecursion -> 0

MaxMemoryUsed[
p3 = ContourPlot3D[(Sin[x^2] + 2)*(Sin[y^2] + 2)*(Sin[z^2] + 2)
, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}
, MaxRecursion -> 0]
] // N@UnitConvert[Quantity[#, "Bytes"], "Megabytes"] &
p3

Quantity[30.8063, "Megabytes"] MaxMemoryUsed drops from 800MB to 30MB with detail reduced.

With PerformanceGoal -> "Speed"

MaxMemoryUsed[
p2 = ContourPlot3D[(Sin[x^2] + 2)*(Sin[y^2] + 2)*(Sin[z^2] + 2)
, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}
, PerformanceGoal -> "Speed"]
] // N@UnitConvert[Quantity[#, "Bytes"], "Megabytes"] &
p2

Quantity[3.16101, "Megabytes"] MaxMemoryUsed drops from 800MB (default) or 30MB (MaxRecursion) to 3MB with detail taking a significant hit but still enough to see the what is going on.

Hope this helps.

• This helped a lot, thanks! I can now explore 3D sine functions further. Apr 3, 2022 at 23:49