1

I'm trying to plot this:

i = 0.01;

ListLinePlot[Table[Cos[x]^p, {x, 0, π/2, i}, {p, 0, 1, i}], 
             ImageSize -> 400, PlotRange -> {{0, 100}, {0, 1}}]


ListLinePlot[Table[Cos[p x], {x, 0, π/2, i}, {p, 0, 1, i}], 
             ImageSize -> 400,   PlotRange -> {{0, 100}, {0, 1}}]

When i = 0.001, it takes a lot of time to render and my computer become unusable. Is there something I can do for rendering it faster or at least making the process slower so I can use my computer normally?

I have a Nvidia GTX460 video board and I know about the CUDA link, but if it's not asking too much, I'd like to get advice from someone who has more experience than me at such matters.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Red Banana
  • 5,329
  • 2
  • 29
  • 47
  • With i=.001 it takes about one minute on my Mac, no freeze or slowdown. What are you times? – A.G. Dec 31 '13 at 05:40
  • 3
    i = 0.001;ListLinePlot[Table[Cos[x]^p, {x, 0, [Pi]/2, i}, {p, 0, 1, i}], ImageSize -> 400, PlotRange -> {{0, 100}, {0, 1}}, MaxPlotPoints -> 100]` :D – Dr. belisarius Dec 31 '13 at 06:00
  • 3
    Replacing Table by ToPackedArray@Table more than doubles the speed. (using Needs["Developer"]`) – A.G. Dec 31 '13 at 06:13
  • You can also do Interpolate@Table[Cos[p x], {x, 0, π/2, i}, {p, 0, 1, i}] and then plot it using Plot. This is much better if you plan to export it as vector graphics, such as EPS because the file size will be much smaller. – C. E. Dec 31 '13 at 10:41
  • 1
    btw. I think an answer explaining this would be useful. I've heard several people complain that MMA renders their computers unusable when it's computing, but this has never happened to me. But I have never seen an answer that talks about why this happen: Is it the OS that doesn't reserve enough of something? (I use OS X), is it the memory that runs out? The processor only has one kernel? MMA on some OSs uses several processor kernels automatically? It would be useful to be able to explain this to people, not only suggest a workaround. – C. E. Dec 31 '13 at 12:03
  • In my experience, on OS X the only problem is when available memory runs out. Sometimes the only solution is hard reboot because the OS will not register any mouse clicks or keypresses. – shrx Dec 31 '13 at 12:39
  • @Anon There are answers here that discuss it, but don't explicitly say that it can hang your system. As a simple example, try this in a fresh session and note the memory usage after the m1 = and m2 = steps: m1 = RandomInteger[100, {4000, 1000, 100}]; f[x_] := x^2; m2 = MapThread[(f /@ #) #2 &, {m1, Range@Length@m1}];. The memory usage jumps up 3-4x for me. Now you might know why this happens, but most people don't. You can imagine that if you're working on a machine with low resources or if f were more time consuming, there's a good chance your system is going to slow down a lot. – rm -rf Dec 31 '13 at 15:29
  • @rm-rf I wasn't particularly interested in why the system hangs if it runs out of memory. It's def. enough to know that memory is causing the problems. People I've spoken to often blame their CPUs or GPUs or the way MMA is constructed (which is partly true, because one common reason is because they've used an expression that won't evaluate with Nest or equivalent) etc. they never think to check their system's vitals so it's hard to tell them what the issue is. – C. E. Dec 31 '13 at 15:50
  • @rm-rf Maybe your comment "if f were more time consuming" hits the nail on the head in regards to what I don't understand. For me, I can calculate something that will take one minute or 24 hours, it still won't take up more than about a quarter of my processing power. Why do time consumption matter for slow downs? And even on computers with low resources, can MMA really use up the entire CPU power even if the code isn't parallelized? Or does time consumption up the amount of memory used? – C. E. Dec 31 '13 at 18:12
  • @Anon In my example, the memory issue is caused by unpacking, not the complexity of the operation. (see What is a Mathematica packed array?.) This is very subtle and can catch many good programmers off-guard. On the face of it, you've done everything seemingly correct – used RandomInteger's extra arguments instead of Table, used Map, MapThread instead of For loops... but MapThread unpacks, which blows up the memory usage. My example only unpacks 1 level, so it could be much worse. – rm -rf Dec 31 '13 at 18:27
  • 1
    @Anon The point about a time consuming f was to point out that during the map thread operation, the peak memory usage might be high enough that if f is slow, you're not going to free up that resource until the computation is finished (or you get a chance to repack it). Also see Are there guidelines for avoiding the unpacking of a packed array? – rm -rf Dec 31 '13 at 18:27

1 Answers1

2

Not sure if this is avoiding the basic question, but for small i you are plottting an excessive number of p points, this is a good bit faster (for small i)

i = .001
Show[MapIndexed[
        ParametricPlot[{p/i, Cos[#]^p} , {p, 0, 1}, 
        AspectRatio -> 1/GoldenRatio, 
        PlotStyle -> ColorData["Rainbow", (First@#2)/(Pi/2/i)]] &, 
        Table[x, {x, 0, Pi/2, i}]], PlotRange -> {0, 1}] 

(note this construct proves to be a bit faster than ParametricPlot[Table[ , {x,..} ] , {p,0,1}] )

It looks nicer to specify the colors as well.. useColorData[1, .. ] to reproduce the default scheme.

george2079
  • 38,913
  • 1
  • 43
  • 110