Re: Speed of writing in Mathematica 3.01
- To: mathgroup@smc.vnet.net
- Subject: [mg11777] Re: [mg11752] Speed of writing in Mathematica 3.01
- From: Sean Ross <seanross@worldnet.att.net>
- Date: Sat, 28 Mar 1998 00:25:15 -0500
- References: <199803260809.DAA06287@smc.vnet.net.>
James Bridges wrote: > > Am I doing something wrong or is it supposed to take orders of magnitude > longer than is reasonable to write out a simple ASCII file from > Mathematica? Consider this simple test: > > In[7]:= > a=Table [i,{i,1,1001}]; > > In[8]:= > Timing[ > filename="Macintosh HD:test.ascii"; > output=OpenWrite[filename]; > Do[ > Write[output,a[[i]]]; > ,{i,1,1001}]; > Close[output]; > ] > > Out[8]= > {23.4 Second,Null} > > 23 seconds to write 1001 integers?! On a 233MHz G3! Everything else is > whiff-bam fast, but writing is glacial. Now this is a simplified > example of what I need to do but it contains one of the factors, namely > that I have to individually write out elements of a list, hence the Do > loop. Any hints? > > James Bridges > > -- > James Bridges > Cleveland, OH Reading and writing files has always been slow in Mathematica. The time to write or read seems to depend on the numbers of objects Mathematica is expected to deal with. Your example does seem abysmally slow. The same code requires 0.22 seconds to execute on my Win95 pentium 166. Usually, Mac's are faster than PC's. I wonder if there is not some conflict in your system or something misallocated among system resources that mathematica depends on. Aside from that, however, there is a trick that I use to speed up file access. Mathematica slows way down if it has to deal with many objects, so I only give it one by converting whatever I want to write to file to a string. For example: In[2]:= a=Table [i,{i,1,1001}]; In[4]:= Timing[ filename="test.Mathematica.txt"; output=OpenWrite[filename]; Do[ Write[output,a[[i]]]; ,{i,1,1001}]; Close[output]; ] Out[4]= {0.22 Second,Null} but: In[5]:= as=ToString[a]; In[7]:= Timing[ filename="teststring.Mathematica.txt"; output=OpenWrite[filename]; Write[filename,as]; Close[output]; ] Out[7]= {0.05 Second,Null} In this case, converting to a string first results in a better than 400% increase in speed. The two files are a little different since the second stores all the commas and brackets associated with the array. If you get rid of those, the writing is even faster: In[5]:= as=ToString[a]; as=StringReplace[as,{","->" ","{"->"","}"->""}]; In[7]:= Timing[ filename="teststring.Mathematica.txt"; output=OpenWrite[filename]; Write[filename,as]; Close[output]; ] Out[7]= {0.0 Second,Null} I haven't yet figured out how to write return characters to ASCII files. The "\n" doesn't work since it is copied literally into the file. Even FromCharacterCode[10] doesn't put a "real" return character in the file, only a bunch of \n's. Anyway, when you read the files, you can manipulate the strings with the various commands then convert it to an expression using ToExpression. -- Remove the _nospam_ in the return address to respond.
- References:
- Speed of writing in Mathematica 3.01
- From: bridges@en.com (James Bridges)
- Speed of writing in Mathematica 3.01