如何在windows应用程序c#中计算程序的执行时间? [英] How to calculate the execution time of the program in windows application c#?

查看:164
本文介绍了如何在windows应用程序c#中计算程序的执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中实现了三种算法,我想根据执行时间比较这些算法,请给我在c#windows窗口中比较这些算法的代码?我该怎么比较?我真的需要它

解决方案

你可以使用秒表 [ ^ ]为此。这样的事情:



  var  watch = Stopwatch.StartNew() ; 
// 您要衡量的代码来这里
watch.Stop ();
var elapsedMs = watch.ElapsedMilliseconds;


SA Kryukov写道:

真正的准确性比那更好......

-SA

会员11063279问:

你的意思是什么?请解释一下?

请参阅:

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.ishighresolution(v = vs.110).aspx [ ^ ] ,

http: //msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.frequency(v=vs.110).aspx [ ^ ]。



调用这两个属性,你会看到。因此,在精确的时间内,使用双重属性

http://msdn.microsoft.com/en-us/library/system.timespan.totalseconds(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/ en-us / library / system.timespan.totalminutes(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.timespan.totalmilliseconds(v = VS .110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.timespan.totalhours(v = vs.110) .aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.timespan.totaldays(v = vs.110).aspx [ ^ ],以获得适当的具有适当精度的小数值,而不是 int 属性毫秒等,带有舍入错误



-SA

I have a three algorithms implemented in c#, i want to compare these algorithm on the basis of the execution time, please give me the code that compare these algorithm in c# windows form?? an how can i compare it?? i really need it

解决方案

You can use Stopwatch [^]for this. Something like this:

var watch = Stopwatch.StartNew();
// the code that you want to measure comes here
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;


S A Kryukov wrote:

Real accuracy is better than that...
—SA

Member 11063279 asked:

what did you mean? please explain?

Please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.ishighresolution(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.frequency(v=vs.110).aspx[^].

Call these two properties and you will see. Therefore, for a precise time, use double properties
http://msdn.microsoft.com/en-us/library/system.timespan.totalseconds(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan.totalminutes(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan.totalmilliseconds(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan.totalhours(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan.totaldays(v=vs.110).aspx[^], to get proper fractional value with appropriate accuracy, instead of int properties Seconds, Milliseconds, etc., with their rounding errors.

—SA


这篇关于如何在windows应用程序c#中计算程序的执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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