2
$\begingroup$

As far as I understand, the Binary Data feature of Mathematica can efficiently store raw numbers or elements of a given type to binary files. Is there a way to store a large symbolic expression (read - wild mixture of all possible element types) in a binary format that can be quickly saved and loaded back into memory?

$\endgroup$
4
  • 4
    $\begingroup$ Why not use DumpSave? $\endgroup$
    – xzczd
    Jun 20, 2018 at 12:38
  • 1
    $\begingroup$ The wdx format is an option: reference.wolfram.com/language/ref/format/WDX.html $\endgroup$ Jun 20, 2018 at 14:50
  • 1
    $\begingroup$ @GustavoDelfino actually BinarySerialize uses the WXF format. $\endgroup$
    – rhermans
    Jun 20, 2018 at 14:55
  • 1
    $\begingroup$ I have not checked recent versions but WDX used to be way slower than other options. Is this still the case? If it is just for quickly saving and loading on the same computer/Mathematica version then DumpSave or export to MX format seems to be by far the best bet... $\endgroup$ Jun 21, 2018 at 12:29

1 Answer 1

3
$\begingroup$

Look at BinarySerialize, particularly the option PerformanceGoal -> "Size". It's even better than Compress

data = {
   RandomReal[1, {10, 10000}],
   ExampleData[{"TestImage", "House"}],
   ExampleData[{"Dataset", "Planets"}],
   ExampleData[{"Text", "UNHumanRightsEnglish"}]
   };

ByteCount /@ Through[{BinarySerialize, Compress}[data]]
(* {1027513, 1220720} *)


DumpSave["test1.mx", data];

dataz = BinarySerialize[data, PerformanceGoal -> "Size"];

DumpSave["test2.mx", dataz];

FileSize /@ {"test1.mx", "test2.mx"}
(* {Quantity[1.0258, "Megabytes"],  Quantity[0.915118, "Megabytes"]} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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