You can also use numpy's csv writter to export large files, on my computer it is more than 10 times faster than the built in Export function for csv files.
The following implementation writes a binary file to disk in Mathematica, then reads it to python and then writes it to a csv file.
writeLargeTableToCSV[file_, data_] :=
Module[{tmp, sess, convertBinaryToCSV},
sess = StartExternalSession["Python"];
ExternalEvaluate[sess, "import numpy"];
convertBinaryToCSV = ExternalFunction[sess, "
def addtwo(in_file,size,out_file):
d = numpy.fromfile(in_file,dtype=numpy.int32).reshape(*size)
numpy.savetxt(out_file, d, fmt='%i', delimiter=',')"
];
tmp = CreateFile[] <> ".bin";
BinaryWrite[tmp, Flatten[largeTable], "Integer32"];
Close[tmp];
convertBinaryToCSV[tmp, Dimensions@data, file];
DeleteObject[sess];
file
]
Even it read/write three times, the performance is still more than 9 times faster.
rows = 1000000;(*smaller for benchmark*)
largeTable = Table[{k, 0, 0}, {k, rows}];
Export["largeTable.csv", largeTable, "CSV"] // AbsoluteTiming
(*{9.40056, "largeTable.csv"}*)
writeLargeTableToCSV["largeTable_from_python.csv",largeTable] // AbsoluteTiming
(*{1.06015, "largeTable_from_python.csv"}*)
The full data can be exported to csv in about 1.5 minutes.
rows = 100000000; largeTable = Table[{k, 0, 0}, {k, rows}];
writeLargeTableToCSV["largeTable_from_python.csv", largeTable] // AbsoluteTiming
{82.5153, "largeTable_from_python.csv"}
Note that int32 type is used so that the exported file is identical to that from builtin Export function. You need to change to float32 for floating numbers, but the performance is similar.