I'm trying to optimize the performance of creating a lot of small files to a SSD disk.
ConcurrentBag<string[]> cb = new ConcurrentBag<string[]>();
cb.AsParallel().ForAll(fa => File.WriteAllText(fa[0], fa[1]));
Total count of the ConcurrentBag<string[]> = 80048, cb.Sum(gbc => Encoding.UTF8.GetByteCount( gbc[1] ) ); returns 393441217 bytes.
Somewhere else I do a xml.Save();, which creates a ~750MB file.
The first situation takes 3 minutes and 30 seconds to complete. The second 20 seconds.
I understand there is some overhead to handle all the seperate write operations but 3 minutes and 30 seconds still seems a bit long. I already tried parallelization with forall, which helped pretty good (before that it took between 6-8 minutes to complete). What other modifications could I add to my code to optimize performance of the bulk file creation?
Actually, multiple simultaneous IO operations can slow things down quite a lot, especially on traditional disks. I recommend using
ConcurrentQueuefor writing multiple files.Also you could switch to
StreamWriterand control buffer size to increase write speed: