且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用C#在多个线程中读取多个文件,慢!

更新时间:2023-01-13 09:24:57

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

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.