Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Huge List<double> save in small file

Tags:

c#

I am trying to save a huge list of doubles in a file. For now it looks like this:

try{
       FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.OpenOrCreate);                   
       using(BinaryWriter binaryWriter = new BinaryWriter(fs))
       {
             foreach (double value in bigData)
             {
                  binaryWriter.Write(value);
             }
             binaryWriter.Close();
        }
        fs.Close();
} catch(System.IO.FileNotFoundException)
{
    MessageBox.Show("Unexpected save error\n", "Save error!", MessageBoxButtons.OK);
}

bigData is a List<double> and in test case it contains 2 millions objects.

The saved file has around 15MB, which I think is quite a lot for only binary data. Has anyone got any idea, how I can make it much smaller?

Also, remember, that I need to open this file after saving - but this is done in another method.

like image 625
sebap123 Avatar asked Jan 22 '26 03:01

sebap123


2 Answers

A double is 8 bytes long, so 2 million times 8 is about 16MB. Seems OK.

like image 165
Bart Friederichs Avatar answered Jan 23 '26 16:01

Bart Friederichs


The saved file has around 15MB, which I think is quite a lot for only binary data.

Well, a double is 8 bytes of data:

The Double value type represents a double-precision 64-bit number

You've got 2 million of them, so that means 16 million bytes. Seems about right to me.

Perhaps you actually want float values instead? That would save half the size... at the cost of precision and range, of course.

Compressing the data may help, but it may not - it depends on whether or not it contains a lot of repetitive information. You may find that compressing it increases the size rather than decreasing it - that's just the nature of having that many possible values.

Without knowing more about your context, we can't tell whether you really have 15MB of useful information or whether there's natural redundancy.

like image 27
Jon Skeet Avatar answered Jan 23 '26 15:01

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!