By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

Fastest way to save an array of doubles on disk?

P: n/a
The title says it all. Anyone?
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Tales Normando" <ta*****@bol.com.br> wrote in message
news:ea**************@TK2MSFTNGP11.phx.gbl...
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
Nov 21 '05 #2

P: n/a
On 2004-11-15, Tales Normando <ta*****@bol.com.br> wrote:
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.

--
Tom Shelton [MVP]
Nov 21 '05 #3

P: n/a
Will the double values be able to be read using NotePad? I kind of forgot to
add that exigence to my post.

"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> escreveu na mensagem
news:OO**************@TK2MSFTNGP09.phx.gbl...
On 2004-11-15, Tales Normando <ta*****@bol.com.br> wrote:
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.

--
Tom Shelton [MVP]

Nov 21 '05 #4

P: n/a
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

"Tales Normando" <ta*****@bol.com.br>
Will the double values be able to be read using NotePad? I kind of forgot
to
add that exigence to my post.

Nov 21 '05 #5

P: n/a
In article <#K**************@TK2MSFTNGP09.phx.gbl>, Tales Normando wrote:
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?

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 23 Days, 21 Hours, 23 Minutes, 47 Seconds
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.