Improving performance with multi-threading on a multi-core CPU

Posted on October 20th, 2009 by David Luhman and tagged , .

The other day I was downloading several podcasts from the net, while doing an expensive rsync across my slow home network. My dual core CPU was showing 50% on one CPU, about 0% on the other, and my network was shown at about 1%.

My iTunes had a "download multiple streams" checkbox that was unchecked. I thought running multiple streams might actually slow things down due to thrashing/context switching, but I checked it to give me multiple streams.

Suddenly my first CPU jumped up to 80%, my second CPU jumped up to 50%, and my network jumped from 1% to 5%. My download speeds visibly increased.

Sometimes, maybe most of the time, multi-threading on a multi-core CPU will speed things up in an unexpected manner.

In another project, a process of archiving (bzipping) a large number of files, the code was rewritten from a single thread to four threads on a four-CPU machine with four cores per CPU for 16 cores :

Original code :
* 78451 files bzipped in 11 hours – 118/min
* 72912 expired files deleted in 3 hours, 50 minutes – 317/min

Parallelized code :
* 78451 files bzipped in 2 hours 36 minutes – 500/min
* 42646 expired files deleted in 14 minutes – 3046/min