C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
Performance Impact with Multiple Threads
This section provides a tutorial example on how to show the performance impact of running multiple threads in a single application.
To find out the performance impact of running multiple threads in a single application, I wrote the following program:
// MultiThread.cs // Copyright (c) 2010 HerongYang.com. All Rights Reserved. using System.Threading; public class MultiThread { public static void Main() { int count = 32; Thread t; for (int n=1; n<=count; n++) { t = new Thread(Run); t.Start(); System.Console.WriteLine(System.DateTime.Now.TimeOfDay +": Launched thread "+n); } } public static void Run() { while(true); } }
I compiled it with .NET 4.6.1 C# compiler, executed it on my Windows 7 system, and got this output:
C:\herong>set "NET=\windows\Microsoft.NET\Framework\v4.0.30319" C:\herong>%NET%\csc MemoryCrash.cs Microsoft (R) Visual C# Compiler version 4.6.1055.0 C:\herong>MemoryCrash C:\herong\cSharp_20100000\cod>MultiThread 15:37:25.1617418: Launched thread 1 15:37:25.1677422: Launched thread 2 15:37:25.1977439: Launched thread 3 15:37:25.2767484: Launched thread 4 15:37:25.3437522: Launched thread 5 15:37:25.4117561: Launched thread 6 15:37:25.5107618: Launched thread 7 15:37:25.6077673: Launched thread 8 15:37:25.7377748: Launched thread 9 15:37:25.8657821: Launched thread 10 ... 15:37:32.0201341: Launched thread 30 15:37:32.5021617: Launched thread 31 15:37:32.9831892: Launched thread 32
As a comparison, here is the result from my test with .NET 4.0 on my Windows XP system with "count = 16;".
C:\herong>MultiThread 21:52:46.2343750: Launched thread 1 21:52:46.3437500: Launched thread 2 21:52:46.4687500: Launched thread 3 21:52:46.7656250: Launched thread 4 21:52:47.0781250: Launched thread 5 21:52:47.5468750: Launched thread 6 21:52:48.0156250: Launched thread 7 21:52:48.6406250: Launched thread 8 21:52:49.3437500: Launched thread 9 21:52:50.1406250: Launched thread 10 21:52:50.9218750: Launched thread 11 21:52:51.3750000: Launched thread 12 21:52:52.8750000: Launched thread 13 21:52:54.0156250: Launched thread 14 21:52:55.2031250: Launched thread 15 21:52:56.5781250: Launched thread 16
Some interesting notes on the execution:
Table of Contents
Logical Expressions and Conditional Statements
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
Threads to Run Instance Methods
►Performance Impact with Multiple Threads
Multi-Thread Programs on Multi-CPU Systems
Maximum Number of Threads in a Program
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation