同时对多个文件进行大量写操作对性能优化

来源:互联网 发布:win7部分软件乱码 编辑:程序博客网 时间:2024/05/20 02:53
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
我自己的一个项目,需要同时对65536个文件进行多次写操作。

如果先全部打开所有的文件,然后重复写,最后关闭所有的文件。那么第一次写操作全部完成需要16分钟左右,而第二次就需要40分钟了。没有继续测试了。


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();
}


如果在写操作的时候只打开相应的一个文件,写完关闭。那么所有写操作完成只要2分30秒左右。


循环

for (int i=0;i<65536;i )
{
open;
write;
close;
}


由此可见,第二种办法性能要远大于第一种。一次打开所有的文件,需要占用不小的内存,最主要的是.net在处理filestream 的时候,可能要进行大量的内存分配和回收等工作,消耗了大量内存和资源。

另外我也做个测试,如果文件数目比较小,那么第一种的性能又要大大好于第二种。


http://xiyangwushi.cnblogs.com/archive/2006/06/28/437409.html

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>