Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use multithreading for multiple file copies

I have to copy large number of files (10000 files)

because it takes long time to copy. I have tried using two threads instead of single thread, one to copy odd number files in list and other to copy even numbers from list

I have used this code:

ThreadPool.QueueUserWorkItem(new WaitCallback(this.RunFileCopy),object)

but there is no significant difference in time when using single thread and when using two threads.

What could be the reason for this?

like image 736
tilanka kulatunge Avatar asked Feb 11 '23 17:02

tilanka kulatunge


1 Answers

File copying is not a CPU process, it a IO process, so multithreding or parallelism wont help you.

Multithreading will slow you down in almost all cases.If disc is SSD too it has a limited speed for r/w and it will use it efficiently with single thread too. If u use parallelism you will just split your speed into pieces and this will create a huge overhead for HDD

Multithreading only helps you in more than one disc case, when you read from different discs and write to different discs.

If files are too small. Zipping and unzipping the files on the target drive can be faster in most cases, and if u zip the files with low compression it will be quite faster

using System.IO;
using System.IO.Compression;

.....

string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
string extractPath = @"c:\example\extract";

ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);

ZipFile.ExtractToDirectory(zipPath, extractPath);

More implementation details here

How to: Compress and Extract Files

like image 96
accfews Avatar answered Feb 15 '23 10:02

accfews