473,805 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing binary array directly to binarywriter

I need to write a data array out to disk, but unfortunately am running
into performance issues. The function is essentially:

private ushort[,] theInt16Array = new ushort[1200,1600];

public void Save(BinaryWrit er bw)
{
/// Far too slow:
// for (int row = 0; row < Rows; row++)
// {
// for (int column = 0; column < Columns; column++)
// {
// bw.Write(theInt 16Array[row, column]);
// }
// }

/// Changed to:
- byte[] byteArray = new byte[theInt16Array.L ength * 2];
-
- unsafe
- {
- fixed (ushort* ushortArrayPtr = theInt16Array)
- {
- IntPtr ushortIntPtr = new IntPtr(ushortAr rayPtr);
- Marshal.Copy(us hortIntPtr, byteArray, 0, byteArray.Lengt h);
- }
- }

- bw.Write(byteAr ray);

}

(dashes used to ensure formatting)

The new method is about 10 times faster than the nested for-loops, but
I still need to improve on it. The main thing that I can see is the
fact that I have to copy the array every time I want to save it out
(depending on system status, this can be anywhere from 6% to 60% of
total function time). Frankly, there no good reason I can see that I
need to do that other than the fact that the Write function is limited
to writing either a single primitive value or a byte array, and there's
no way (that I know of) to treat theInt16Array as a byte array for the
duration of the write.

So, is there a better way to do this?

--
David

Nov 16 '05 #1
1 4093
Buffer.BlockCop y may be faster; it can copy shorts to bytes. If you are
creating a new byte array each time you might want to make that array static
and reuse it. BinaryWriter.Wr ite can write out any portion of a byte array.

Regards,
Frank Hileman

check out VG.net: http://www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio .NET graphics editor

<ds****@iftd.or g> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
I need to write a data array out to disk, but unfortunately am running
into performance issues. The function is essentially:

private ushort[,] theInt16Array = new ushort[1200,1600];

public void Save(BinaryWrit er bw)
{
/// Far too slow:
// for (int row = 0; row < Rows; row++)
// {
// for (int column = 0; column < Columns; column++)
// {
// bw.Write(theInt 16Array[row, column]);
// }
// }

/// Changed to:
- byte[] byteArray = new byte[theInt16Array.L ength * 2];
-
- unsafe
- {
- fixed (ushort* ushortArrayPtr = theInt16Array)
- {
- IntPtr ushortIntPtr = new IntPtr(ushortAr rayPtr);
- Marshal.Copy(us hortIntPtr, byteArray, 0, byteArray.Lengt h);
- }
- }

- bw.Write(byteAr ray);

}

(dashes used to ensure formatting)

The new method is about 10 times faster than the nested for-loops, but
I still need to improve on it. The main thing that I can see is the
fact that I have to copy the array every time I want to save it out
(depending on system status, this can be anywhere from 6% to 60% of
total function time). Frankly, there no good reason I can see that I
need to do that other than the fact that the Write function is limited
to writing either a single primitive value or a byte array, and there's
no way (that I know of) to treat theInt16Array as a byte array for the
duration of the write.

So, is there a better way to do this?

--
David

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3490
by: Kai Bohli | last post by:
Hi all ! I've come across a huge problem (for me at least). I'm trying to send some initial graphics to a labelprinter. To do this, I load the graphics from resource and send it directly to the printerport along with "printer instructions". The problem is that the printer instruction have to be "plain text" while the image has to be binary. Something like this:
11
11964
by: ajou_king | last post by:
I was running some tests on my Win32 1GHZ processor to see how long it would take to transmit objects numerous times via TCP/IP using C# ..NET Remoting vs the C++ trustworthy method of binary streams. I ran the test for 50K, 100K, 500K iterations, where each iteration consists of sending an object from a client process to a server process, and the server process sends back an ack. Here are the results: .NET Remoting C++...
2
1378
by: Peter Steele | last post by:
I have a managed C++ project that contains a simple structure: struct { <multiple members> } mydata; I want to write the contents of this structure out to disk retaining the exactly binary data without any additional header information, and of course be able to later read it back in. What's the best way to accomplish this?
2
1673
by: tim | last post by:
i have an array of bytes which i write to a binary file seems to me the only way is to write one element at a time, which takes *forever is there a way to write the entire array to the binary file without looping something like binarywriter.writeEntireArray(arrayOfBytes would be perfect!
7
6069
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
2
6797
by: simonc | last post by:
Is there an easy way of writing a number in 32 bit integer format into four bytes of a file? I am experimenting with FilePut but I can only make it work by defining a four byte array and doing some simple calculations to work out from my integer what the individual values of the four bytes have to be. Surely there must be a short cut to doing this? And are there also any existing routines for converting a floating point number into...
3
1245
by: Søren M. Olesen | last post by:
Hi What would be the easiest way, to write an 'AppleDouble file header' to a file, from vb.net?? AppleDouble file header: -------------------------------------------------------------------------------- Field Length -------------------------------------------------------------------------------- Magic number 4 bytes
4
1650
by: Bob Cummings | last post by:
Good Day I would like to write a password to a binary file. I am following along in the book and examples I have found googling. However when I open the file in notepad it looks like a text file. Can someone tell me what I am doing wrong? try { if (sfdCommon.ShowDialog() ==
6
5253
by: aagarwal8 | last post by:
Hi, I am trying to write the contents of a textbox to a file in binary format. My code looks like this... private void btnWriteToFile_Click(object sender, EventArgs e) { FileStream fs = File.Open(@"D:\test.dat", FileMode.OpenOrCreate, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fs);
0
9718
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10614
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.