Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Say I have a For loops the following,

Monitor[For[y = 6, y < 380, y++
   For[v = 5, v < y, v++,
    For[z = 4, z < v, z++,
     For[x = 3, x < z, x++, 
      If[x + z + v - 3 >= 2*y - 2 && 
        Exponent[r[x, z, v, y], A] < (x + z + v - 2)/2,
       Print[{x, z, v, y}, f[x, z, v, y]]]]]]], y]

It'll take lots of time. Is it possible to use the command Parallelize? Or how can I reduce the time?

share|improve this question
You can't auto-parallelize a For loop because it uses mutable state (the iteration variable). As a beginner it's best never to use For anyway---if you are using it, you are very likely doing things in a too complicated or an inefficient manner. Use either Do (for which you have ParallelDo but you still need to avoid changing global variables in the loop) or in your case, use Table, which has the counterpart ParallelTable. – Szabolcs May 14 at 22:04
1  
Nested For loops are about the worst way to go in Mathematica, you are much better off with a single Do or Table statement. I would try that before you worried about parallelizing it (which often has unexpected consequences). – Guillochon May 14 at 22:14
3  
You are asking for nearly 20 x 10^9 iterations. With the If and guessing at the time for f, that's about 10^-7 sec. or more. per iteration on my machine, or for the whole loop something like from a 1/2 hour or more. With f, it will be longer; if the time per iteration is 10^-5 sec., then the whole loop will take more than 2 days! – Michael E2 May 14 at 23:22

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.