Article: Multi-threading on the desktop »
FERDY CHRISTANT - JUL 30, 2007 (06:48:01 PM)
Threading is a common phenomenon on the server. Most web servers, database servers and server-side processes make extensive use of threading to manage load, sessions and resources. With the arrival of desktops and laptops with multiple CPUs, multiple cores and hyperthreading, the same is now possible on the client. Unfortunately, many desktop applications are still written as single-threaded programs. These programs do not make use of the multi-threaded nature of today's hardware, and as a result, they miss out on serious performance benefits. This article is about a threading diagnostic tool called Thread3r that I wrote to analyze the performance benefit of multi-threaded operations on the desktop.
What is Thread3r?
Thread3r is a simple diagnostic tool that allows you to analyze the performance benefit of multi-threaded applications on your machine. It runs a large number of simple mathematical operations and divides this work over a number of threads. You can specify both the number of operations to perform and the number of threads to divide these operations over. Consider the following example. We need to run 50 million calculations. In a single-threaded application, all 50 million operations will be executed sequentially. If we use two threads, each thread will execute 25 million operations. If you have multiple CPUs in your system, both threads can run in parallel on the hardware level. The end result is that the total set of operations (50 million) takes far less time to complete when using two threads on a system with multiple CPUs. In the example scenario, it will run almost twice as fast compared to the single-thread scenario.
Thread3r benchmarks the performance of the total set of operations on your system, based on the amount of operations and threads you specify. You can play around freely with these parameters. The number of operations can range from 1 to 100 million, the amount of threads can range from 1 to 100.
Using Thread3r is extremely simple. First, download the installer and follow the instructions on screen. Note that Thread3r is for Windows only and requires version 2.0 of the .Net framework or later. Once Thread3r is installed, you can launch it from the desktop, or from the Start menu "s3maphor3" group. Thread3r consist of a single small window only, that looks like this:
Let's go over the main elements of the screen. The logo contains a link to my s3maphor3 project site. Next, follows a simple explanation of the program. The "# of operations" allows you to specify the total amount of operations to perform.On fast machines, you may want to provide a much larger number than the default. The "# of threads" value indicates the number of threads to divide the total number of operations over. By default, this is set to the number of CPUs that are detected on the system (green field in the bottom), but you can set any value between 1 and 100. The text box outputs the benchmark results. The "Benchmark!" button will start the benchmark, given your input settings.
To further explain Thread3r, here is a small test run. My system has two CPUs. I will let it perform 5,000,000 (5 million) operations on a single thread. The benchmark result is:
Benchmark started for 2 CPU(s), 1 thread(s), please be patient...
Thread 1 started.
Thread 1 ended.
Benchmark results (hh:mm:ss:ms) : 00:00:11.020
Benchmark ended for 2 CPU(s), 1 thread(s), 5000000 operations.
Just over 11 seconds. Now let's do these 50 million operations again using two threads. The benchmark result is:
Benchmark started for 2 CPU(s), 2 thread(s), please be patient...
Thread 1 started.
Thread 2 started.
Thread 1 ended.
Thread 2 ended.
Benchmark results (hh:mm:ss:ms) : 00:00:07.028
Benchmark ended for 2 CPU(s), 2 thread(s), 5000000 operations.
Just over 7 seconds! Running the same amount of operations on multiple threads is 4 seconds faster (63%) than running it on a single thread. Also note the difference in CPU usage. The single-threaded scenario shows a steep peek in CPU usage, but only in one CPU. The second peek is the multi-threaded run which fully utilizes both CPUs:
If you further play with Thread3r you will encounter some interesting results. For example, you might expect that using more than two threads will increase the performance even more, but this is not always the case. I hope this little tool helps you in becoming aware of the advantages of multi-threaded applications versus single-threaded applications. Note that the decision to go multi-threaded in your application should be taken on a case by case basis. Large sets of CPU-intensive operations benefit from threading, whilst smaller sets of operation and I/O operations in general provide far less benefit.
The making of Thread3r
Thread3r, and the Thread3r installer are built in Visual Studio using the .Net framework 2.0. It makes use of the framework's Threader class to dynamically instantiate new threads. The UI is a single Winform. You can find more info about Thread3r on the project homepage. Enjoy!