One of my own projects requires multiple write operations to 65536 files at the same time.
If you open all files first, then write repeatedly, and finally close all files. Then it will take about 16 minutes for the first write operation to be completed, and it will take 40 minutes for the second time. No further testing.
for (int i = 0; i < 65536; i++)
{
fileStream[i] = new FileStream(buffDir+"\"+ i.ToString() + ".dat", FileMode.Create,FileAccess.Write, FileShare.Write,14000);
}
write;
write;
write;
........
for (int i = 0; i < 65536; i++)
{
fileStream[i] .close();
}
If only one corresponding file is opened during the writing operation, it will be closed after writing. Then it only takes about 2 minutes and 30 seconds for all write operations to be completed.
cycle{
for (int i=0;i<65536;i++)
{
open;
write;
close;
}
}
It can be seen that the performance of the second method is much greater than the first one. Opening all files at once requires a lot of memory. The most important thing is that when .net processes filestream, it may have to perform a lot of memory allocation and recycling, which consumes a lot of memory and resources.
In addition, I also did a test. If the number of files is relatively small, then the performance of the first method is much better than the second one.
http://xiyangwushi.cnblogs.com/archive/2006/06/28/437409.html