0
$\begingroup$

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]]
| improve this question | | | | |
$\endgroup$
  • 3
    $\begingroup$ 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. $\endgroup$ – Dr. belisarius Sep 22 '14 at 18:09
  • 2
    $\begingroup$ See for example mathematica.stackexchange.com/a/18396/193 $\endgroup$ – Dr. belisarius Sep 22 '14 at 18:12
  • 2
    $\begingroup$ 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. $\endgroup$ – george2079 Sep 22 '14 at 18:33
1
$\begingroup$

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

| improve this answer | | | | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.