且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在windows应用程序c#中计算程序的执行时间?

更新时间:2023-11-23 11:39:28

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



  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