我试图了解使用常用 Thread
之间的好处s 和 Task
s。第一个进入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 需要两次创建两种对象的刻度数,创建线程和提供的委托(delegate)的实际执行之间的刻度,在我的例子中是
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 |
也就是说,还有一些补充说明:ThreadPool.UnsafeQueueWorkItem
相反(它允许您在没有任务 API 的情况下使用线程池)关于c# - 使用 c# 的 Thread 和 Task 之间的性能比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52914450/