473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Fastest way to save an array of doubles on disk?

The title says it all. Anyone?
Nov 21 '05 #1
5 4467
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: pfemat | last post by:
Hi there, i am doing some heavy computations whose result i want to save to harddisk. the results are in an long array named screen and i thought of doing char *tmp =...
11
by: Ignacio X. Domínguez | last post by:
Hi. I'm developing a desktop application that needs to store some data in a local file. Let's say for example that I want to have an address book with names and phone numbers in a file. I would...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
14
by: romayankin | last post by:
Hello All, I'm writing cross-platform code so i'm bound to standards. Here is the code I have: ~~~~~~~~~~~~~~~~~~ double **mx = new double*; for(int i = 0; i < col - 1; i++) { mx = new...
9
by: sshock | last post by:
Hi all, I want to read from a file into a vector<unsigned char>. Right now my code looks like this: FILE* f = fopen( "datafile", "rb" ); enum { SIZE = 100 }; vector<unsigned char>...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
1
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
15
by: Buddy Home | last post by:
Hello, I'm trying to speed up a piece of code that is causing performance issues with our product. The problem is we are using serialization to convert the object to a string, this is costing us...
6
by: elet.mirror | last post by:
Hi everyone, I am currently using Visual C++ 8 Standard. I have a quite large data array (1GB) in the form of an unsigned char array. I need to save this to disk in 16-bit format (two chars...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.