Intermediate ExportString
makes "CSV"
exports much faster
I actually can reproduce this extraordinary slow CSV export on my two systems, on a "9.0 for Microsoft Windows (32-bit) (January 24, 2013)"
even when I work locally (see below), and on a "9.0 for Mac OS X x86 (64-bit) (January 24, 2013)"
when I work over a network share (not shown here):
t = RandomReal[{0, 1000}, {4000, 80}];
Export[FileNameJoin[{$TemporaryDirectory, "Matrix.CSV"}], t, "CSV"]
This may take eons. Let's see how long it takes:
{FileExtension@#, First@AbsoluteTiming[Export[#, t, FileExtension@#]],
FileByteCount@#} & /@ {FileNameJoin[{$TemporaryDirectory,
"Matrix.CSV"}]} // Grid
CSV 159.78120 5815838
To me, this amazingly long time seems to be caused by laborious interactions with the file system, in particular I suspect every line of the CSV is being sent separately. That is why I propose a work around, where firstly all data is "CSV"
ed using ExportString
and secondly is exported using the simple "Table"
formatting, which seems to work en bloc:
Export[FileNameJoin[{$TemporaryDirectory, "Matrix.CSV"}], ExportString[t, "CSV"], "Table"]
This appears to work much faster, let's see how long it takes this time:
{FileExtension@#,
First@AbsoluteTiming[
Export[#, ExportString[t, FileExtension@#], "Table"]],
FileByteCount@#} & /@ {FileNameJoin[{$TemporaryDirectory,
"Matrix.CSV"}]} // Grid
CSV 10.234375 5815838
About 16 times faster. Enjoy!
Edit
I made the same test on an ancient "7.0 for Mac OS X PowerPC (32-bit) (February 19, 2009)"
, interestingly here the direct "CSV"
Export (to $TemporaryDirectory
) is approx. 10% faster than my work around. Hmm. In contrary, on the abovementioned "9.0 for Mac OS X x86 (64-bit) (January 24, 2013)"
system it is 30% faster. But for network shares my work around anyway is highly recommended for comma separated value exports!