使用c#的Thread和Task的性能比较
Performance comparison between Thread and Task using c#
我正在尝试了解使用通用 Thread
和 Task
之间的好处。第一个进入 System.Threading
命名空间,后者进入 System.Threading.Tasks
命名空间。
所以,为了玩和熟悉它们,我在 C#
:
中编写了这个程序
class Program
{
static void Main(string[] args)
{
long ticksAtStart = DateTime.UtcNow.Ticks;
new Thread(() => { ExecuteAsyn("Thread", DateTime.UtcNow.Ticks); }).Start();
Console.WriteLine("Using Thread: " + (DateTime.UtcNow.Ticks - ticksAtStart));
ticksAtStart = DateTime.UtcNow.Ticks;
Task g = Task.Factory.StartNew(() => ExecuteAsyn("TPL", DateTime.UtcNow.Ticks));
Console.WriteLine("Using TPL: " + (DateTime.UtcNow.Ticks - ticksAtStart));
g.Wait();
Console.ReadKey();
}
private static void ExecuteAsyn(string source, long ticksAtExecutionTime)
{
Console.WriteLine("Hello World! Using " + source + " the difference between initialization and execution is " + (DateTime.UtcNow.Ticks - ticksAtExecutionTime));
}
}
因此,根据我的理解,任务应该具有更高的性能,因为它们使用 ThreadPool
中可用的线程,而创建和启动新线程可能会非常耗费资源。
该程序记录两个事件。 CRL 花费两次创建两种对象和创建线程与实际执行提供的委托之间的滴答数,在我的例子中 ExecuteAsync
.
我没想到的事情发生了:
Using Thread: 11372 Hello World! Using Thread the difference between
initialization and execution is 5482 Using TPL: 333004 Hello World!
Using TPL the difference between initialization and execution is 0
所以看起来使用经典线程比使用任务更高效。
但对我来说这里有些奇怪。任何人都可以启发我这个话题吗?
谢谢。
第一次执行一个方法的成本总是更高:程序集是延迟加载的,方法可能还没有被 JITted。
例如,如果我们采用您的基准(为了精确,将 DateTime 替换为 Stopwatch)并再次调用 Task.Factory.StartNew
:
static void Main(string[] args)
{
var sw = Stopwatch.StartNew();
new Thread(() => { ExecuteAsyn("Thread", sw); }).Start();
Console.WriteLine("Using Thread: " + sw.Elapsed);
sw = Stopwatch.StartNew();
Task g = Task.Factory.StartNew(() => ExecuteAsyn("TPL", sw));
Console.WriteLine("Using TPL: " + sw.Elapsed);
g.Wait();
sw = Stopwatch.StartNew();
g = Task.Factory.StartNew(() => ExecuteAsyn("TPL", sw));
Console.WriteLine("Using TPL: " + sw.Elapsed);
g.Wait();
Console.ReadKey();
}
private static void ExecuteAsyn(string source, Stopwatch sw)
{
Console.WriteLine("Hello World! Using " + source + " the difference between initialization and execution is " + (sw.Elapsed));
}
我电脑上的结果是:
Using Thread: 00:00:00.0002071
Hello World! Using Thread the difference between initialization and execution is 00:00:00.0004732
Using TPL: 00:00:00.0046301
Hello World! Using TPL the difference between initialization and execution is 00:00:00.0048927
Using TPL: 00:00:00.0000027
Hello World! Using TPL the difference between initialization and execution is 00:00:00.0001215
我们可以看到第二次调用比第一次调用快了三个数量级。
使用真正的基准框架(例如,BenchmarkDotNet),我们可以获得更可靠的结果:
Method | Mean | Error | StdDev |
-------- |-----------:|----------:|----------:|
Threads | 137.426 us | 1.9170 us | 1.7932 us |
Tasks | 2.384 us | 0.0322 us | 0.0301 us |
也就是说,还有一些补充说明:
你的比较不公平。您正在比较线程的创建与通过任务 APIs 在线程池上排队任务。为了公平起见,您应该改用 ThreadPool.UnsafeQueueWorkItem
(这允许您在没有任务 API 的情况下使用线程池)
使用Thread还是Task应该不是性能问题。这确实是一个方便的问题。性能差距不太可能对您的应用程序产生任何影响,除非您正在处理低延迟或非常高的吞吐量。
我正在尝试了解使用通用 Thread
和 Task
之间的好处。第一个进入 System.Threading
命名空间,后者进入 System.Threading.Tasks
命名空间。
所以,为了玩和熟悉它们,我在 C#
:
class Program
{
static void Main(string[] args)
{
long ticksAtStart = DateTime.UtcNow.Ticks;
new Thread(() => { ExecuteAsyn("Thread", DateTime.UtcNow.Ticks); }).Start();
Console.WriteLine("Using Thread: " + (DateTime.UtcNow.Ticks - ticksAtStart));
ticksAtStart = DateTime.UtcNow.Ticks;
Task g = Task.Factory.StartNew(() => ExecuteAsyn("TPL", DateTime.UtcNow.Ticks));
Console.WriteLine("Using TPL: " + (DateTime.UtcNow.Ticks - ticksAtStart));
g.Wait();
Console.ReadKey();
}
private static void ExecuteAsyn(string source, long ticksAtExecutionTime)
{
Console.WriteLine("Hello World! Using " + source + " the difference between initialization and execution is " + (DateTime.UtcNow.Ticks - ticksAtExecutionTime));
}
}
因此,根据我的理解,任务应该具有更高的性能,因为它们使用 ThreadPool
中可用的线程,而创建和启动新线程可能会非常耗费资源。
该程序记录两个事件。 CRL 花费两次创建两种对象和创建线程与实际执行提供的委托之间的滴答数,在我的例子中 ExecuteAsync
.
我没想到的事情发生了:
Using Thread: 11372 Hello World! Using Thread the difference between initialization and execution is 5482 Using TPL: 333004 Hello World! Using TPL the difference between initialization and execution is 0
所以看起来使用经典线程比使用任务更高效。 但对我来说这里有些奇怪。任何人都可以启发我这个话题吗? 谢谢。
第一次执行一个方法的成本总是更高:程序集是延迟加载的,方法可能还没有被 JITted。
例如,如果我们采用您的基准(为了精确,将 DateTime 替换为 Stopwatch)并再次调用 Task.Factory.StartNew
:
static void Main(string[] args)
{
var sw = Stopwatch.StartNew();
new Thread(() => { ExecuteAsyn("Thread", sw); }).Start();
Console.WriteLine("Using Thread: " + sw.Elapsed);
sw = Stopwatch.StartNew();
Task g = Task.Factory.StartNew(() => ExecuteAsyn("TPL", sw));
Console.WriteLine("Using TPL: " + sw.Elapsed);
g.Wait();
sw = Stopwatch.StartNew();
g = Task.Factory.StartNew(() => ExecuteAsyn("TPL", sw));
Console.WriteLine("Using TPL: " + sw.Elapsed);
g.Wait();
Console.ReadKey();
}
private static void ExecuteAsyn(string source, Stopwatch sw)
{
Console.WriteLine("Hello World! Using " + source + " the difference between initialization and execution is " + (sw.Elapsed));
}
我电脑上的结果是:
Using Thread: 00:00:00.0002071
Hello World! Using Thread the difference between initialization and execution is 00:00:00.0004732
Using TPL: 00:00:00.0046301
Hello World! Using TPL the difference between initialization and execution is 00:00:00.0048927
Using TPL: 00:00:00.0000027
Hello World! Using TPL the difference between initialization and execution is 00:00:00.0001215
我们可以看到第二次调用比第一次调用快了三个数量级。
使用真正的基准框架(例如,BenchmarkDotNet),我们可以获得更可靠的结果:
Method | Mean | Error | StdDev |
-------- |-----------:|----------:|----------:|
Threads | 137.426 us | 1.9170 us | 1.7932 us |
Tasks | 2.384 us | 0.0322 us | 0.0301 us |
也就是说,还有一些补充说明:
你的比较不公平。您正在比较线程的创建与通过任务 APIs 在线程池上排队任务。为了公平起见,您应该改用
ThreadPool.UnsafeQueueWorkItem
(这允许您在没有任务 API 的情况下使用线程池)使用Thread还是Task应该不是性能问题。这确实是一个方便的问题。性能差距不太可能对您的应用程序产生任何影响,除非您正在处理低延迟或非常高的吞吐量。