C# has poor performance on large file ?

C

chris chan

I just want to create a large file with size of 80MB
and add a char 1 at the end.
Two method I has been used.
i) seek to the End and write one char
ii) open the file , and append it to the end

both method take nearly 30sec to do so
I want to ask whether they is other method can do this job
using a shorter time ?

Thx
Chris


FileStream fs = new FileStream(filename, FileMode.Open);
fs.SetLength(size);
fs.Close();

fs = new FileStream(filename,
FileMode.Append,FileAccess.Write,FileShare.None,16384 );
byte c =1;
fs.WriteByte(c);
fs.Close();
 
J

Jon Skeet

chris chan said:
I just want to create a large file with size of 80MB
and add a char 1 at the end.
Two method I has been used.
i) seek to the End and write one char
ii) open the file , and append it to the end

both method take nearly 30sec to do so
I want to ask whether they is other method can do this job
using a shorter time ?

That sounds rather long. Could you write a short but complete program
which demonstrates the problem, and post it? I'll have a look then.
 
J

Jon Skeet

chris chan said:
i have attached both exe and the sourse file

First thing: there's no need to write a GUI to make a simple test case.
In this case, it would have been a *very* short console app.

Anyway, I don't get the same results as you - both take very little
time at all.
 
C

chris chan

I don't understand why it happen yet
and from the above post
Jon Skeet has tested it is both finish in a short time
may be It is my computer problem ~~
but after reinstall the .Net framework
the problem is still here

I just can figure out which part take so long time to finish
when i run the Step mode
It seem that c# speed a lot of time on
fs.Close() this line
and I separate it in two
fs.Flush()
and fs.Close()

Most of the time is speed on fs.Flush if there is change of file happen
if no change of file context, it can be finish in 10 ms
but if there is some change , even it is just 1 byte change
it will use 30 sec to Flush

it is some strange by the way ~-

http://new.dyndns.ws:5050/announce
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top