Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whether there should be a difference to copy files in parallel and not in parallel?

I tried to copy 200 files with both below solutions but I didn't saw the difference ( I used System.Diagnostics.Stopwatch to measure time). In both cases it took 8 seconds. Should not the second(Parallel) solution be faster? I thought because it is IO operations using Parallel will speed up the copy.

What I'm missing?

// Case1 - Regular iteration
foreach (FileInfo file in files)
{
    string temppath = Path.Combine(destDirName, file.Name);
    file.CopyTo(temppath, false);
}

// Case2 - Parallel
Parallel.ForEach(files, file =>
{
    string temppath = Path.Combine(destDirName, file.Name);
    file.CopyTo(temppath, false);
});
like image 268
theateist Avatar asked Oct 24 '12 09:10

theateist


People also ask

What is parallel copy?

Auxiliary Copy Parallel Copy allows you to define a storage policy copy to be configured with a parallel copy, which specifies that after reading the source copy (only once) data can be written to multiple secondary copies concurrently rather than sequentially.


1 Answers

Parallel execution of tasks doesn't guarantee performance improvements.

In your case, file copying will likely be IO bound, not CPU bound. The CPU typically has much more bandwidth available than the IO device (unless you happen to be using a Fusion IO card or something), so IO actions tend to cause the CPU to wait a lot.

Tasks that use a lot of CPU can be done in parallel to get performance gains. Tasks that also wait on external factors can also benefit from being shifted onto another thread so as not to become a blocking task.

However, tasks that are waiting for the same external resource will likely see little benefit unless that external resource can itself handle the traffic associated with multiple threads (IO tends to never be able to handle the traffic and could in fact cause contention of the resource, slowing it down).

like image 123
Adam Houldsworth Avatar answered Sep 30 '22 05:09

Adam Houldsworth