0

I am using For loop to calculate a value $z$ in every cycle. I would like to create a text file, save the current value $z$ in this file and afterwards delete the value from Mathematica's memory. I would also like to do it as fast as possible.

Example of my loop:

For[x=0,x<=1,x=x+0.5;z=Exp[x^2]]
infinity
  • 395
  • 1
  • 8
  • 3
    For[] and similar loops are very inefficient in Mathematica. Why don't you add what are you trying to achieve instead of focusing in the function you want to use. – Dr. belisarius Sep 22 '14 at 18:09
  • 2
    See for example http://mathematica.stackexchange.com/a/18396/193 – Dr. belisarius Sep 22 '14 at 18:12
  • 2
    do you really need to Write in the loop? It would almost certainly be faster to save the data and write it out all at once. – george2079 Sep 22 '14 at 18:33

1 Answers1

1

First of all avoid using For loop better (and faster) use Do instead.

stream = OpenWrite["c:\\1.txt"];
Do[
  z = Exp[x^2];
  Write[stream, z]
  , {x, 0, 100000, 0.5}];
Close[stream];

PS: it is best to avoid cycles for functional analog

molekyla777
  • 2,888
  • 1
  • 14
  • 28