473,395 Members | 1,631 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.

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(BinaryWriter bw)
{
/// Far too slow:
// for (int row = 0; row < Rows; row++)
// {
// for (int column = 0; column < Columns; column++)
// {
// bw.Write(theInt16Array[row, column]);
// }
// }

/// Changed to:
- byte[] byteArray = new byte[theInt16Array.Length * 2];
-
- unsafe
- {
- fixed (ushort* ushortArrayPtr = theInt16Array)
- {
- IntPtr ushortIntPtr = new IntPtr(ushortArrayPtr);
- Marshal.Copy(ushortIntPtr, byteArray, 0, byteArray.Length);
- }
- }

- bw.Write(byteArray);

}

(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 4029
Buffer.BlockCopy 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.Write 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.org> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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(BinaryWriter bw)
{
/// Far too slow:
// for (int row = 0; row < Rows; row++)
// {
// for (int column = 0; column < Columns; column++)
// {
// bw.Write(theInt16Array[row, column]);
// }
// }

/// Changed to:
- byte[] byteArray = new byte[theInt16Array.Length * 2];
-
- unsafe
- {
- fixed (ushort* ushortArrayPtr = theInt16Array)
- {
- IntPtr ushortIntPtr = new IntPtr(ushortArrayPtr);
- Marshal.Copy(ushortIntPtr, byteArray, 0, byteArray.Length);
- }
- }

- bw.Write(byteArray);

}

(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
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...
11
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...
2
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...
2
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...
7
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...
2
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...
3
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:...
4
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...
6
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 =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.