捕获准确的执行时间 [英] capturing the accurate execution time

查看:83
本文介绍了捕获准确的执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我尝试捕捉功能的确切执行时间



Hi all,

I try to capture the exact execution time of function

Stopwatch regularSW = new Stopwatch();
for (int i = 0; i < 10; i++){
regularSW.Start();
//function();
regularSW.Stop();

Console.WriteLine("Measured time: " + regularSW.Elapsed);
}





还尝试使用DateTime和Process.GetCurrentProcess()。TotalProcessorTime

And每次我得到不同的价值。



如何获得相同的价值?



Also tried with DateTime and with Process.GetCurrentProcess().TotalProcessorTime
And each time i get different value.

How i can get same value ?

推荐答案

所花的时间对于要执行的程序取决于环境,时间差将永远不会与您的计算机上已经运行的n个应用程序相同,CPU正忙于执行那些正在运行的应用程序并且在获取执行请求时意味着。网络应用也是如此,所以时差将会存在并且非常小。所以不要担心时差。

如果你真的想得到程序执行的大约时间,那么每次运行应用程序5次并记录时差,最后找到平均持续时间。这将是你的实际时差。
The time taken for a program to execute is depend on the environment, time difference will never be the same as there are n number of application already running on your machine, CPU is busy of executing those running application and mean while it gets a request to execute the .net application also, So the time difference will be there and it is very minimal. So don't worry with that time difference.
If you really want to get the approx time duration of the program execution, then run the application 5 times and record time difference each time and at last find the average time duration. That will be your actual time difference.


你将永远不会通过这种方法两次获得相同的时间,你永远无法测量确切的时间。在最好的情况下,您可以平均数千次迭代的时间。

想一想:调用秒表,计时器的分辨率和操作系统中未知的其他活动都有管理费用。



有分析: http://csapp.cs.cmu.edu/public/1e/public /ch9-preview.pdf [ ^ ]



阅读本文 - 它可能有所帮助:如何:用C#测量执行时间 [ ^ ]
You will never get the same time twice by this method and you can never measure the exact time. At the very best you can average the time for thousands of iterations.
Think about it: There are overheads to calling the Stopwatch, resolution of the timer and unknown other activities in the OS.

There is analysis here: http://csapp.cs.cmu.edu/public/1e/public/ch9-preview.pdf[^]

Read this article - it may help: How To: Measure execution time in C#[^]


是的。你不能得到相同的值。它取决于您的系统CPU使用率。
Yes it is.You can't get Same Values .Its depends upon your System CPU Usage.


这篇关于捕获准确的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆