cody powell dot com / projects / ThreadTest

Recently at work, we got into a spirited discussion via email about the performance enhancements that can be obtained through multi-threading in the .NET Framework. If you read that sentence and think to yourself, "Those dudes know how to rock," you've definitely got a point.

There were a lot of opinions exchanged, but one that kept coming up was that threading actually slows down an application because it makes inefficient use of the processor. I'm no threading master; while I can write a good multi-threaded app, I'd rather tickle a rhino's behind than try to compute the Big O notation for multi-threaded, computationally intensive code. However, as a lazy programmer, I knew I could write up a proof-of-concept to evaluate the idea. That's just what I did.

Ladies and gents, I offer you ThreadTest. Not only is it a pretty good introduction to threading in C#, it outputs performance information for a computationally-intensive task (determining the factors for a huge number) that's done once in a single thread and once in a multi-thread environment.

The EXE is meant to be run from the command line. You must supply it with two parameters: the number to be factored and the number of threads to spin off. When you run it (by typing something like TestThread 1000000000 5), it'll factor the number once in a single thread, and then it'll break the number into chunks, spin off the desired number of threads, and let each thread factor a chunk. Once completed, it tells you how long each method took, as well as which was faster.

It's pretty fun to play around with, plus it helps to provide some hard data on multi-threading. The results I obtained showed that a threaded, computationally-intensive task can perform in a fraction of the time that the same non-threaded, computationally-intensive can perform in. For a simple task, any gains to be had from threading are negated due to the complexity of spinning off the threads. Altering the number of threads didn't seem to have as large a performance effect as one might think.

Here are a few sample results I obtained on my single-processor laptop. This is all extremely unscientific, and only tried on one machine.

Factor Value Threads Non-Threaded Ticks Threaded Ticks Winner
100 5 0 100144 Non-Threaded
1000 5 0 200288 Non-Threaded
1000000 (10^6) 5 500720 300432 Threaded
1000000000 (10^9) 5 253865040 130487632 Threaded
1000000000000 (10^10) 5 5167129968 377442736 (7% of the threaded time) Threaded
1000000000000 (10^10) 10 513918792 376441296 (also 7% of the threaded time) Threaded