473,383 Members | 1,925 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,383 software developers and data experts.

FileStream.Write slow to write last 100K of 2GB file

Is there a faster way to write the last 100K of a large file? This code
takes almost two minutes to run on my machine.

int buffer = 100000;
int length = 2000000000;
string file = @"C:\test.txt";

if (File.Exists(file))
File.Delete(file);

int s = Environment.TickCount;
string test = "".PadLeft(buffer, 'a');
Byte[] data = Encoding.ASCII.GetBytes(test);
FileStream fs = File.Open(file, FileMode.OpenOrCreate);

int s2 = Environment.TickCount;
fs.SetLength(length);
System.Diagnostics.Debug.WriteLine("set: " + (Environment.TickCount - s2));

s2 = Environment.TickCount;
fs.Seek(length - buffer, SeekOrigin.Begin);
System.Diagnostics.Debug.WriteLine("seek: " + (Environment.TickCount - s2));

s2 = Environment.TickCount;
fs.Write(data, 0, buffer);
System.Diagnostics.Debug.WriteLine("write: " + (Environment.TickCount -
s2));

s2 = Environment.TickCount;
fs.Flush();
System.Diagnostics.Debug.WriteLine("flush: " + (Environment.TickCount -
s2));

fs.Close();
System.Diagnostics.Debug.WriteLine("complete: " + (Environment.TickCount -
s));
Thanks,
Eric
Jun 16 '06 #1
5 4224

I tried your code... (Pentium IV 1,7 Ghz / 512Mb Ram / HD = IDE 7200rpm)

For a 100K file : set: 0 / seek: 0 / write: 0/ flush: 0 / complete: 15
(Immediate)

int buffer = 100000;

int length = 100005;

For a 2Gb File : set: 0 / seek: 0 / write: 40766 / flush: 0 / complete:
40781 (About 40Seconds)

int buffer = 100000;

int length = 2000000000;

Maybe the problem is your machine....

Steph.

"Eric Cadwell" <ec******@com.com> wrote in message
news:ew**************@TK2MSFTNGP05.phx.gbl...
Is there a faster way to write the last 100K of a large file? This code
takes almost two minutes to run on my machine.

int buffer = 100000;
int length = 2000000000;
string file = @"C:\test.txt";

if (File.Exists(file))
File.Delete(file);

int s = Environment.TickCount;
string test = "".PadLeft(buffer, 'a');
Byte[] data = Encoding.ASCII.GetBytes(test);
FileStream fs = File.Open(file, FileMode.OpenOrCreate);

int s2 = Environment.TickCount;
fs.SetLength(length);
System.Diagnostics.Debug.WriteLine("set: " + (Environment.TickCount - s2));
s2 = Environment.TickCount;
fs.Seek(length - buffer, SeekOrigin.Begin);
System.Diagnostics.Debug.WriteLine("seek: " + (Environment.TickCount - s2));
s2 = Environment.TickCount;
fs.Write(data, 0, buffer);
System.Diagnostics.Debug.WriteLine("write: " + (Environment.TickCount -
s2));

s2 = Environment.TickCount;
fs.Flush();
System.Diagnostics.Debug.WriteLine("flush: " + (Environment.TickCount -
s2));

fs.Close();
System.Diagnostics.Debug.WriteLine("complete: " + (Environment.TickCount -
s));
Thanks,
Eric

Jun 16 '06 #2
I'm running Centrino Duo 2Ghz, 2GB ram, 7200 RPM with Media Center Edition.
I would expect a millisecond response. If I write the file sequentially it's
sub-second all the way.

Thanks,
Eric
Jun 16 '06 #3
What you expect is not possible when writing to a newly created file.
When a file does not allready exists, all the file data must be written
starting from offset 0, so what you are measuring is effectively the time it
takes to write 2Gb of data.
If the file allready exists, writing 100Kb at the end will only take a
fraction of a second.
You can try this by commenting this part of your code when running a second
time:
if (File.Exists(file))
File.Delete(file);

Willy.


"Eric Cadwell" <ec******@com.com> wrote in message
news:ew**************@TK2MSFTNGP05.phx.gbl...
| Is there a faster way to write the last 100K of a large file? This code
| takes almost two minutes to run on my machine.
|
| int buffer = 100000;
| int length = 2000000000;
| string file = @"C:\test.txt";
|
| if (File.Exists(file))
| File.Delete(file);
|
| int s = Environment.TickCount;
| string test = "".PadLeft(buffer, 'a');
| Byte[] data = Encoding.ASCII.GetBytes(test);
| FileStream fs = File.Open(file, FileMode.OpenOrCreate);
|
| int s2 = Environment.TickCount;
| fs.SetLength(length);
| System.Diagnostics.Debug.WriteLine("set: " + (Environment.TickCount -
s2));
|
| s2 = Environment.TickCount;
| fs.Seek(length - buffer, SeekOrigin.Begin);
| System.Diagnostics.Debug.WriteLine("seek: " + (Environment.TickCount -
s2));
|
| s2 = Environment.TickCount;
| fs.Write(data, 0, buffer);
| System.Diagnostics.Debug.WriteLine("write: " + (Environment.TickCount -
| s2));
|
| s2 = Environment.TickCount;
| fs.Flush();
| System.Diagnostics.Debug.WriteLine("flush: " + (Environment.TickCount -
| s2));
|
| fs.Close();
| System.Diagnostics.Debug.WriteLine("complete: " + (Environment.TickCount -
| s));
|
|
| Thanks,
| Eric
|
|
Jun 16 '06 #4
Thanks Willy,
Is there any way to interrupt it? If I want to close the application while
it's allocating space it takes two minutes to shut down.

-Eric
Jun 16 '06 #5
No, the file "creation" is a single IO action, and on current version of
Windows this cannot be canceled. The question is: why do you need to create
a 2GB file if you only wan't to write 100Kb real data to it?

Willy.
"Eric Cadwell" <ec******@com.com> wrote in message
news:Oa**************@TK2MSFTNGP04.phx.gbl...
| Thanks Willy,
| Is there any way to interrupt it? If I want to close the application while
| it's allocating space it takes two minutes to shut down.
|
| -Eric
|
|
Jun 16 '06 #6

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

Similar topics

6
by: Tony C | last post by:
Does Python have a function that is analogous to C's write() or fwrite()- that is , I want to write a file (of arbitrary data) that is 100K, or 1MB (or more) bytes long.. Both write() and...
1
by: Viorel | last post by:
About FileStream and StreamWriter!? I have a string1 which contains(of course in unicode) a html code with Unicode charset specified in it. 1) bytecontent=...
11
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
5
by: Mike Dole | last post by:
When I am updating a picturebox with an image (extracted) from an access database I keep getting a "The process cannot access the file "c:\foto1.jpg because it is being used by another process"...
3
by: Terry | last post by:
In the code below, I open a file using filestream, copy it to a byte array, and the write that array out to a new file. But when I check the size of the original file and the new file, the new...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
6
by: bonk | last post by:
I am trying to create a stream that writes text to a file and: - automatically creates a new file once the current file exceeds a certain size - makes it possible to be used by multiple threads...
8
by: Andreas Zimmermann | last post by:
Hi, we just set up a SQL Server 2008 on Windows Server 2008 (configured as file / webserver, 16 GB memory, 4 dual core processors, 6 internal disks + 15 disc ISCSI array with overall almost 3 TB...
4
by: Tony Johansson | last post by:
Hello! Here I have an small piece of code from a program. I have read about the last parameter of GetBytes but I can't really understand it's use. I mean that the GC would take care of...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.