如何在windows应用程序c#中计算程序的执行时间? [英] How to calculate the execution time of the program in windows application 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...
—SAMember 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 ofint
propertiesSeconds
,Milliseconds
, etc., with their rounding errors.
—SA
这篇关于如何在windows应用程序c#中计算程序的执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!