473,757 Members | 10,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary file writing problem

Hi,
I've got a program (see source below) that makes a file and fills it with
random binary values (from 0 to 255). The source below works, however the
program creates files at a rate of about 0.5MB per second. There is a
serious performance issue with this program.
There is a loop within the main loop that generates a buffer of 500 Bytes
that are then all written at once.
Can anybody tell me how I can improve the performance of the program? Idealy
I could with a rate of about 25Mb/s.

Thanx Josh

('reps' is the number of repetitions. e.g. Number of bytes)

FileStream fs = new FileStream(file Location, FileMode.Create New);

// Create the writer for data.

BinaryWriter w = new BinaryWriter(fs );

Random r =new Random();

for (int i = 0; i < Int32.Parse(rep s.ToString()); )

{

byte[] bits = new byte[500];

for (int j = 0; j <= 499; j++)

{

bits[j] = byte.Parse(r.Ne xt(0, 255).ToString() );

i++;

if(i >= Int32.Parse(rep s.ToString()))

{

break;

}

}

w.Write(bits);

}
Nov 16 '05 #1
3 2734
Not sure what disk IO subsystem you are running this on, but don't ever
expect to achieve 25MB write throughput on anything less than a RAID0 (HW)
with a minimum of 7-8 spindles.
As a simple rule take a max. sustained IO rate of ~80-100 IO writes per
second per (7200 RPM) spindle.
So 500Kb per sec. is a fair number on a single drive with a block size of
500 bytes, what you should try first is to increase the buffer size to
something like 4-8 Kb, but don't expect higher throughputs than 2 - 3 Mb per
sec.

Willy.

source below) that makes a file and fills it with
random binary values (from 0 to 255). The source below works, however the
program creates files at a rate of about 0.5MB per second. There is a
serious performance issue with this program.
There is a loop within the main loop that generates a buffer of 500 Bytes
that are then all written at once.
Can anybody tell me how I can improve the performance of the program?
Idealy
I could with a rate of about 25Mb/s.

Thanx Josh

('reps' is the number of repetitions. e.g. Number of bytes)

FileStream fs = new FileStream(file Location, FileMode.Create New);

// Create the writer for data.

BinaryWriter w = new BinaryWriter(fs );

Random r =new Random();

for (int i = 0; i < Int32.Parse(rep s.ToString()); )

{

byte[] bits = new byte[500];

for (int j = 0; j <= 499; j++)

{

bits[j] = byte.Parse(r.Ne xt(0, 255).ToString() );

i++;

if(i >= Int32.Parse(rep s.ToString()))

{

break;

}

}

w.Write(bits);

}

Nov 16 '05 #2
Hello
for (int i = 0; i < Int32.Parse(rep s.ToString()); ) I understand that reps is a variable of type int. why convert it to a string
and back to int again? and this takes place at every iteration.
for(int i = 0; i < reps; i++) will do
bits[j] = byte.Parse(r.Ne xt(0, 255).ToString() ); Again converting to string and back to byte the following is much better.
bits[j] = (byte)r.Next(0, 255);

i++;
if(i >= Int32.Parse(rep s.ToString()))
{
break;
}

The above is not needed, the check is redundant since you do the check in
the for statement, and again you convert to string and parse the string.
Also although i++ would work here, but its most appropriate place would be
in the for loop itself.

Converting an integer to a string includes allocation of a new string, and
the process writing the string representation of int in the allocated
string. The allocation will make garbage collections occur more frequently.
Converting from string to int is parsing each character in the string, and
checking if it is a valid number and calculating the resulting string. These
2 steps harm the performance badly in your case, and

The bits array is allocated reps times, I would allocate it once outside the
outer loop and reuse it to avoid allocations and garbage collections. Also
the BinaryReader itself is not usefull in your case, because you are writing
a byte array which can be done with FileStream. And since BinaryReader and
FileStream classes provides buffers, so you don't need your own buffer. You
can use WriteByte and FileStream will take care of buffering.

The program would be

FileStream fs = new FileStream(file Location, FileMode.Create New);
Random r =new Random();
int iterations = reps * 500;
for (int i = 0; i < iterations;i++ )
{
fs.WriteByte((b yte)r.Next(0, 255));
}
Nov 16 '05 #3
Forget this, as Sherif correctly states the Filestream is buffered, so you
shouldn't buffer yourself.
Applying the changes as pointed out by Sherif, a sustained transfer rate of
10MB/sec should be possible on a single drive.
25 MB will however require a RAID0 stripe set of 2-3 drives.

Willy.

"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:%2******** *******@TK2MSFT NGP11.phx.gbl.. .
Not sure what disk IO subsystem you are running this on, but don't ever
expect to achieve 25MB write throughput on anything less than a RAID0 (HW)
with a minimum of 7-8 spindles.
As a simple rule take a max. sustained IO rate of ~80-100 IO writes per
second per (7200 RPM) spindle.
So 500Kb per sec. is a fair number on a single drive with a block size of
500 bytes, what you should try first is to increase the buffer size to
something like 4-8 Kb, but don't expect higher throughputs than 2 - 3 Mb
per sec.

Willy.

Nov 16 '05 #4

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

Similar topics

103
48743
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
3
2128
by: John R. Delaney | last post by:
I am running in debugging mode after a clean C++ compilation under .NET 2003. In a BIG loop (controlled many levels up in the call stack), I open a file with fopen using the "a" option. Then I write 23 doubles to it with fwrite, one call for each double. Then I close the file using fclose. After three times around the loop in the debugger, I stop the program (using "Stop debugging"). That is writing 552 bytes. The resulting file's properties...
5
5313
by: Neo | last post by:
Hello: I am receiving a Binary File in a Request from a application. The stream which comes to me has the boundary (Something like "---------------------------39<WBR>­0C0F3E0099" without the quotes), and also some more text like this and file name (e.g. "Content-Disposition: form-data; name="upload_file"; filename="C:\testing\myfile.da<WBR>­t" Content-Type: application/octet-stream")
4
3695
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the following code that compiles OK. It runs and shows the requested output. However, after execution, it pops one of those windows to send error reports online to the porgram creator. I have managed to find where the error is but can't see what's wrong....
3
18961
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its original form. The problem is tha the binary source that I extract from the text file seems to be diferent from the source I saved. Here is my code: 1) handle=file('image.gif','rb')
15
2412
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with two int32's as its data members, the binary file of that class must have a size of 8 bytes (i.e. only contains class data members, not methods etc.). Is serialization the answer to the above problem? If I understand correctly, the reason that...
15
3001
by: JoeC | last post by:
I am writing a program that I am trying to learn and save binary files. This is the page I found as a source: http://www.angelfire.com/country/aldev0/cpphowto/cpp_BinaryFileIO.html I have successfully created and used txt files. I am trying to save then load in an array of pointers to objects:
2
3545
by: Thomi Aurel RUAG A | last post by:
Hy I'm using Python 2.4.2 on an ARM (PXA-270) platform (linux-2.6.17). My Goal is to write a list of bytes down to a file (opened in binary mode) in one cycle. The crux is that a '0x0a' (line feed) will break the cycle of one writing into several pieces. Writing to a "simple" file, this wouldn't cause any problem. Assuming - without questioning ;-) - that a device file (/dev/*) has to be written in one cycle because one write call will...
13
18199
by: zach | last post by:
Can someone help me out, I can't figure out what I'm doing wrong to write to a file in binary mode. What's wrong with my code? <?php $fileName = "something.dat"; $string = "This is a string of text";
5
3809
by: zehra.mb | last post by:
Hi, I had written application for storing employee data in binary file and reading those data from binary file and display it in C language. But I face some issue with writing data to binary file. Here is my part of my code. struct cust_data { int nCAFID; char *szFirstName;
0
9489
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
9298
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
10072
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
9906
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
9737
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6562
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
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3829
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
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.