使用C#在多个线程中读取多个文件,慢! [英] Reading Multiple Files in Multiple Threads using C#, Slow !

查看:831
本文介绍了使用C#在多个线程中读取多个文件,慢!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Intel Core 2 Duo CPU,我正在从我的 C:驱动器中读取3个文件,并将
个文件中的一些匹配值显示到EditBox上在屏幕上显示。整个过程需要2分钟。然后,我想到了在单独的线程中处理每个文件,然后整个过程需要2.30分钟!!!也就是说,比单线程处理多30秒。

I have an Intel Core 2 Duo CPU and i was reading 3 files from my C: drive and showing some matching values from the files onto a EditBox on Screen.The whole process takes 2 minutes.Then I thought of processing each file in a separate thread and then the whole process is taking 2.30 minutes !!! i.e 30 seconds more than single threaded processing.

我期望反过来!我可以在CPU使用率历史记录中看到两个图表。有些人请向我解释一下正在进行 ?
这是我的代码段。

I was expecting the other way around !I can see both the Graphs in CPU usage history.Some one please explain to me what is going on ? here is my code snippet.

 foreach (FileInfo file in FileList)
{

   Thread t  = new Thread(new ParameterizedThreadStart(ProcessFileData));
   t.Start(file.FullName);  

}

其中 processFileData 是处理文件的方法。

where processFileData is the method that process the files.

谢谢!

推荐答案

问题的根源在于文件位于同一驱动器上,与双核处理器不同,您的硬盘一次只能做一件事。

The root of the problem is that the files are on the same drive and, unlike your dual core processor, your hard drive can only do one thing at a time.

如果同时读取两个文件,则磁盘头将从一个文件跳转到另一个文件,然后再返回。既然您的硬盘驱动器可以在大约40秒内读取每个文件,那么它现在就具有额外的开销,即在读取过程中,磁盘头在三个单独的文件之间多次移动。

If you read two files simultaneously, the disk heads will jump from one file to the other and back again. Given that your hard drive can read each file in roughly 40 seconds, it now has the additional overhead of moving its disk head between the three separate files many times during the read.

从单个硬盘驱动器读取多个文件的最快方法是在一个线程中完成所有操作,然后一个接一个地读取它们。这样,磁头每个文件读取(在开始时)仅移动一次,而不是每次读取多次。

The fastest way to read multiple files from a single hard drive is to do it all in one thread and read them one after another. This way, the head only moves once per file read (at the very beginning) and not multiple times per read.

要优化此过程,您要么需要更改逻辑(您真的需要读取所有三个文件的全部内容吗?)。或购买速度更快的硬盘驱动器/将3个文件放入三个不同的硬盘驱动器中,并使用线程/ RAID。

To optimize this process, you'll either need to change your logic (do you really need to read the whole contents of all three files?). Or purchase a faster hard drive/put the 3 files in three different hard drives and use threading/use a raid.

这篇关于使用C#在多个线程中读取多个文件,慢!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆