Fastest way to save an array of doubles on disk?

  • Thread starter Thread starter Tales Normando
  • Start date Start date
Tales Normando said:
The title says it all. Anyone?

I'm not sure you want the _very_ fastest way.

The array of doubles is sitting on the managed heap. To be written to a
file the doubles must be converted to bytes. BitConverter can do this one
double at a time, but this requires copying each double to the stack,
copying it to a new 4-byte array and writing that array to disk. This
generates many more instructions and more memory copying than would be
necessary in, say, C. But CPU speeds and memory speeds are so much faster
than disk IO speeds that the amount of memory copying or looping in code
rarely matters much. Who really cares if your CPU is at 1% as opposed to 5%
when writing to disk?

The very fastest way to do this would probably to open a file mapping
against the target file, get an IntPtr pointer to the desired offset in the
file, and use System.Runtime.InteropServices.Marshal.Copy to copy the array
of doubles to the IntPtr. This would write the array to file with no
looping in managed code absolutely no intermediate copying. But that's a
giant hastle and requires a ton of code permissions.

But the easy, safe way performs just fine for almost all purposes:

Dim f As New System.IO.FileStream("out.bin", IO.FileMode.OpenOrCreate,
IO.FileAccess.Write)
Dim br As New System.IO.BinaryWriter(f)
For Each d As Double In dd
br.Write(d)
Next
br.Close()
f.Close()

David
 
The title says it all. Anyone?

hmmm... Serialization maybe?

Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

Public Class App

Public Shared Sub Main ()
Dim doubles() As Double = New Double {1.0, 2.0, 3.0, 4.0, 5.0}
Dim fs As New FileStream ("array.dbl", FileMode.Create)
Dim bf As New BinaryFormatter ()

' Write out the array and close the stream
bf.Serialize (fs, d)
fs.Close ()

' lets open a new stream and read in the array
fs = New FileStream ("array.dbl", FileMode.Open)

Dim anotherDoubles() As Double = _
DirectCast (bf.Deserialize (fs), Double ())

fs.Close ()

' see if we got'em
For Each dbl As Double In anotherDoubles
Console.WriteLine (dbl)
Next dbl

End Sub
End Class

I actually did this in C# first, and sort of add hocked the conversion
here - so, you may have a couple of minor syntax errors :) But, the
concept should be obvious.
 
Will the double values be able to be read using NotePad? I kind of forgot to
add that exigence to my post.
 
Tales,

Why you do not just try that yourself.

However AFAIK do you not have to be afraid for that, it is readable however
in a format that almost nobody understand direct.

Cor
 
Will the double values be able to be read using NotePad? I kind of forgot to
add that exigence to my post.

No. It wouldn't be since I used a binary formatter. What exactly are
your requirements? What format do you want them in?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top