C# I/O Parallelism does increase performance with SSD?

asked9 years ago
last updated9 years ago
viewed5.6k times
Up Vote24Down Vote

I've read some answers ( for example) here at SO where some say that parallelism is not going to increase performance ( maybe in read IO).

But I've created few tests which shows that also WRITE operations are much faster.

I've created random 6000 files with dummy data :

Let's try to read them w/ w/o parallelism :

var files =
    Directory.GetFiles("c:\\temp\\2\\", "*.*", SearchOption.TopDirectoryOnly).Take(1000).ToList();

    var sw = Stopwatch.StartNew();
    files.ForEach(f => ReadAllBytes(f).GetHashCode()); 
    sw.ElapsedMilliseconds.Dump("Run READ- Serial");
    sw.Stop(); 


    sw.Restart();
    files.AsParallel().ForAll(f => ReadAllBytes(f).GetHashCode()); 
    sw.ElapsedMilliseconds.Dump("Run READ- Parallel");
    sw.Stop();

Result1:

Run READ- Serial 595 Run READ- Parallel 193

Result2:

Run READ- Serial 316 Run READ- Parallel 192

Going to create 1000 random files where each file is 300K. (I've emptied the directory from prev test)

var bytes = new byte[300000];
Random r = new Random();
r.NextBytes(bytes);
var list = Enumerable.Range(1, 1000).ToList();

sw.Restart();
list.ForEach((f) => WriteAllBytes(@"c:\\temp\\2\\" + Path.GetRandomFileName(), bytes)); 
sw.ElapsedMilliseconds.Dump("Run WRITE serial");
sw.Stop();

sw.Restart();
list.AsParallel().ForAll((f) => WriteAllBytes(@"c:\\temp\\2\\" + 
Path.GetRandomFileName(), bytes)); 
sw.ElapsedMilliseconds.Dump("Run  WRITE Parallel");
sw.Stop();

Result 1:

Run WRITE serial 2028 Run WRITE Parallel 368

Result 2:

Run WRITE serial 784 Run WRITE Parallel 426

The results have surprised me. It is clear that against all expectations ( especially with WRITE operations)- the performance are better with parallelism , yet with IO operations.

How/Why come the parallelism results better ? It seems that SSD can work with threads and that there is no/less bottleneck when running more than one job at a time in the IO device.