Skip to main content
added 255 characters in body
Source Link
Chris Degnen
  • 31.3k
  • 2
  • 56
  • 109

First rerunning the OP's demo for a baseline timing.

e = RandomReal[{-10, 10}, {1000, 1000}];
AbsoluteTiming[
 Export[FileNameJoin[{$InitialDirectory, "vxFolder", "vx7.txt"}], e, "Table"];
 ]

{34.0312, Null}

As you can see in the code below, most of the time is spent formatting the data but the writing takes only 0.7 seconds. If the OP's matrix can be generated as a string in the right format writing can be done quickly.

AbsoluteTiming[
 str = OpenWrite[FileNameJoin[{$InitialDirectory, "vxFolder", "vx8.txt"}]];
 ]

AbsoluteTiming[
 e2 = StringTake[StringReplace[
     ToString[InputForm@e], {"}, {" -> "\n", ", " -> "\t"}], {3, -3}];
 ]

AbsoluteTiming[
 WriteString[str, e2];
 Close[str];
 ]

{0.00266855, Null}

{27.4393, Null}

{0.684855, Null}

An alternative formatting function takes about the same time.

AbsoluteTiming[
 e2 = StringRiffle[StringRiffle[#, "\t"] & /@ Map[
      StringReplace[Internal`DoubleToString[#], "`" -> ""] &, e, {2}], "\n"];
 ]

{29.589, Null}

First rerunning the OP's demo.

e = RandomReal[{-10, 10}, {1000, 1000}];
AbsoluteTiming[
 Export[FileNameJoin[{$InitialDirectory, "vxFolder", "vx7.txt"}], e, "Table"];
 ]

{34.0312, Null}

As you can see in the code below, most of the time is spent formatting the data but the writing takes only 0.7 seconds. If the OP's matrix can be generated as a string in the right format writing can be done quickly.

AbsoluteTiming[
 str = OpenWrite[FileNameJoin[{$InitialDirectory, "vxFolder", "vx8.txt"}]];
 ]

AbsoluteTiming[
 e2 = StringTake[StringReplace[
     ToString[InputForm@e], {"}, {" -> "\n", ", " -> "\t"}], {3, -3}];
 ]

AbsoluteTiming[
 WriteString[str, e2];
 Close[str];
 ]

{0.00266855, Null}

{27.4393, Null}

{0.684855, Null}

First rerunning the OP's demo for a baseline timing.

e = RandomReal[{-10, 10}, {1000, 1000}];
AbsoluteTiming[
 Export[FileNameJoin[{$InitialDirectory, "vxFolder", "vx7.txt"}], e, "Table"];
 ]

{34.0312, Null}

As you can see in the code below, most of the time is spent formatting the data but the writing takes only 0.7 seconds. If the OP's matrix can be generated as a string in the right format writing can be done quickly.

AbsoluteTiming[
 str = OpenWrite[FileNameJoin[{$InitialDirectory, "vxFolder", "vx8.txt"}]];
 ]

AbsoluteTiming[
 e2 = StringTake[StringReplace[
     ToString[InputForm@e], {"}, {" -> "\n", ", " -> "\t"}], {3, -3}];
 ]

AbsoluteTiming[
 WriteString[str, e2];
 Close[str];
 ]

{0.00266855, Null}

{27.4393, Null}

{0.684855, Null}

An alternative formatting function takes about the same time.

AbsoluteTiming[
 e2 = StringRiffle[StringRiffle[#, "\t"] & /@ Map[
      StringReplace[Internal`DoubleToString[#], "`" -> ""] &, e, {2}], "\n"];
 ]

{29.589, Null}

Source Link
Chris Degnen
  • 31.3k
  • 2
  • 56
  • 109

First rerunning the OP's demo.

e = RandomReal[{-10, 10}, {1000, 1000}];
AbsoluteTiming[
 Export[FileNameJoin[{$InitialDirectory, "vxFolder", "vx7.txt"}], e, "Table"];
 ]

{34.0312, Null}

As you can see in the code below, most of the time is spent formatting the data but the writing takes only 0.7 seconds. If the OP's matrix can be generated as a string in the right format writing can be done quickly.

AbsoluteTiming[
 str = OpenWrite[FileNameJoin[{$InitialDirectory, "vxFolder", "vx8.txt"}]];
 ]

AbsoluteTiming[
 e2 = StringTake[StringReplace[
     ToString[InputForm@e], {"}, {" -> "\n", ", " -> "\t"}], {3, -3}];
 ]

AbsoluteTiming[
 WriteString[str, e2];
 Close[str];
 ]

{0.00266855, Null}

{27.4393, Null}

{0.684855, Null}