63

32

I used the code below (which is a sample from this gist containing more similar code) in my answer to my own question about Mandelbrot-like sets for functions other than the simple quadratic on Math.SE to generate this image:

```
cosineEscapeTime =
Compile[{{c, _Complex}},
Block[{z = c, n = 2, escapeRadius = 10 \[Pi], maxIterations = 100},
While[And[Abs[z] <= escapeRadius, n < maxIterations],
z = Cos[z] + c; n++]; n]]
Block[{center = {0.5527, 0.9435}, radius = 0.1},
DensityPlot[
cosineEscapeTime[x + I y], {x, center[[1]] - radius,
center[[1]] + radius}, {y, center[[2]] - radius,
center[[2]] + radius}, PlotPoints -> 250, AspectRatio -> 1,
ColorFunction -> "TemperatureMap"]]
```

What could I do to improve the speed/time-efficiency of this code? Is there any reasonable way to parallelize it? (I'm running Mathematica 8 on an 8-core machine.)

** edit** Thanks all for the help so far. I wanted to post an update with what I'm seeing based on the answers so far and see if I get any further refinements before I accept an answer. Without going to hand-written C code and/or OpenCL/CUDA stuff, the best so far seems to be to use

`cosineEscapeTime`

as defined above, but replace the `Block[...DensityPlot[]]`

with:```
Block[{center = {0.5527, 0.9435}, radius = 0.1, n = 500},
Graphics[
Raster[Rescale@
ParallelTable[
cosineEscapeTime[x + I y],
{y, center[[2]] - radius, center[[2]] + radius, 2 radius/(n - 1)},
{x, center[[1]] - radius, center[[1]] + radius, 2 radius/(n - 1)}],
ColorFunction -> "TemperatureMap"], ImageSize -> n]
]
```

Probably in large part because it parallelizes over my 8 cores, this runs in a little under 1 second versus about 27 seconds for my original code (based on `AbsoluteTiming[]`

).

Strange,

`Graphics[Raster[Rescale[...]]]`

returns a white image in Mathematica 10 on OS X.`ArrayPlot[Reverse@...]`

works fine. – shrx – 2014-01-30T16:08:47.203Great answer. I'd upvote but I'm out of votes for today. – Mike Bailey – 2012-01-18T05:41:46.877

Thank you, Mike, your answer is great too! This is all compiled from bits of Documentation. I'll post links too. – Vitaliy Kaurov – 2012-01-18T06:09:37.317

If any answer is the "right" one (of which all of these are), this would be the one. Beautifully demonstrates usage of every concept mentioned,

plusit has references to the documentation. – Mike Bailey – 2012-01-19T02:11:07.837I played with this a little using

`AbsoluteTiming[Graphics[Raster[Rescale@ParallelTable[...],ColorFunction -> "TemperatureMap"]]]`

and it seems like your`FixedPoint[]`

formulation of the escape-time function is slower and produces different results than my original`While[]`

formulation, and adding`, CompilationTarget -> "C", RuntimeAttributes -> {Listable}, Parallelization -> True`

inside the`Compile[]`

of my`While[]`

version doesn't make any significant difference. As you suggested, the`Graphics[Raster[Rescale@...]]`

formulation ismuchfaster than`DensityPlot[]`

. – Isaac – 2012-01-19T03:18:41.137With further experimentation, the

`FixedPoint[]`

version does yield the same results as my`While[]`

version (I missed something that caused it not to before), but it is still about 20% slower than the`While[]`

version. – Isaac – 2012-01-19T03:50:49.707@Isaac My guess is that Parallelization and C would start to show advantage for the larger image sizes and more cores present. I just wanted to share different approaches documented in M. Thanks for trying these out ;-) – Vitaliy Kaurov – 2012-01-19T04:05:32.267

1@Isaac at one point I wasted a day or so trying to find the fastest way to obtain the mandelbrot set. The fastest I could do was

`While[(iters < maxiter) && (Abs@z < 2),iters++;z = z^2 + c]`

compiled to C, and this was a bit faster than`FixedPoint`

,`Nest`

and other similar approaches. ie, I found the same as you. – acl – 2012-01-19T11:25:10.563