Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 如何使用Process.GetCurrentProcess()测量时间跨度。线程[0]。UserProcessorTime_Multithreading_C# 4.0_Time_Process - Fatal编程技术网

Multithreading 如何使用Process.GetCurrentProcess()测量时间跨度。线程[0]。UserProcessorTime

Multithreading 如何使用Process.GetCurrentProcess()测量时间跨度。线程[0]。UserProcessorTime,multithreading,c#-4.0,time,process,Multithreading,C# 4.0,Time,Process,为了衡量某些代码的性能。。。我在代码块下面运行 startingTime = Process.GetCurrentProcess().Threads[0].UserProcessorTime; <Some lengthy taks> duration = Process.GetCurrentProcess().Threads[0].UserProcessorTime.Subtract(startingTime); startingTime=Process.GetCurrentPro

为了衡量某些代码的性能。。。我在代码块下面运行

startingTime = Process.GetCurrentProcess().Threads[0].UserProcessorTime;
<Some lengthy taks>
duration = Process.GetCurrentProcess().Threads[0].UserProcessorTime.Subtract(startingTime);
startingTime=Process.GetCurrentProcess().Threads[0].UserProcessorTime;
duration=Process.GetCurrentProcess().Threads[0]。UserProcessorTime.Subtract(启动时间);
但问题是持续时间总是0(零)

我不能使用秒表,因为它将包括系统在其他地方(如GC)或进程之外花费的所有时间


有谁能帮我找出这里出了什么问题。

你应该用秒表来测量经过的时间
UserProcessorTime
将不包括在内核中花费的时间(例如完成API调用)或由于IO阻塞而导致的延迟。在GC中花费的时间在很大程度上是代码性能的一部分(垃圾不是免费的)。如果您试图在其他东西正在运行的环境中进行性能度量,即使CPU时间已计算在内,这些东西的内存和IO开销也会影响代码的性能。与消除外部性相比,将精力投入到更干净的测量环境中更容易。长期运行代码的线程[0]是正确的可能性从来都不是很高。没有简单的方法可以将.NET线程与ProcessThread匹配,需要pinvoke调用GetCurrentThreadId()并将其与ProcessThread.Id匹配。这只能通过修改线程代码本身来实现,你们也可以用秒表来代替。谢谢大家。。。我相信,秒表是衡量时间复杂性的正确方法。我现在就来。你应该用秒表来测量经过的时间
UserProcessorTime
将不包括在内核中花费的时间(例如完成API调用)或由于IO阻塞而导致的延迟。在GC中花费的时间在很大程度上是代码性能的一部分(垃圾不是免费的)。如果您试图在其他东西正在运行的环境中进行性能度量,即使CPU时间已计算在内,这些东西的内存和IO开销也会影响代码的性能。与消除外部性相比,将精力投入到更干净的测量环境中更容易。长期运行代码的线程[0]是正确的可能性从来都不是很高。没有简单的方法可以将.NET线程与ProcessThread匹配,需要pinvoke调用GetCurrentThreadId()并将其与ProcessThread.Id匹配。这只能通过修改线程代码本身来实现,你们也可以用秒表来代替。谢谢大家。。。我相信,秒表是衡量时间复杂性的正确方法。我现在就来。