0
我们正在研究两个不同的库,它们往往会执行相同的工作。我已经被分配来检查哪个库在给定的输入集合下更快地执行相同的工作。有多个测试用例可以确定这一点,我想检查库执行任务需要多少时间(以毫秒为单位)。检查C#代码的性能的最佳方法
以下是代码的概要。
// preparation of input parameters
// Get the timestamp
// Calling the desired library with the set of input parameters
// again get the timestamp
// Process the output received from the library
// get a diff between timestamp and write it into a file.
我应该做的在C#中,并阅读了一些提供文档和一些网上搜索,我想出了下面的代码可用于测量
int s1 = DateTime.Now.Millisecond;
int e1 = DateTime.Now.Millisecond;
int f1 = e1 - s1;
FileStream fs = new FileStream("Time_Diff.txt", FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
string str = Convert.ToString(f1);
sw.WriteLine(str);
sw.WriteLine(e1 + " " + s1);
sw.Flush();
sw.Close();
采取(以毫秒为单位)的时间
我只是想知道我正朝着正确的方向前进。另外如果有人能够建议我采用另一种方式来做同样的事情?
我想知道的另一件事是我可以使用其他实践方法来确保我们选择的图书馆能够提供最佳性能?
另一个小的要求。有人可以让我知道如何实现秒表(或任何其他类似的方式来获得时间戳)在C++中做同样的事情吗?
感谢,如下 Azeem
DIFF是不是一个好主意,因为精度并不好(http://jaychapman.blogspot.co.at/2007/11/datetimenow-precision.html)。 ..使用'秒表'实例!无论如何,只需使用关键字“基准”和“C#”进行复杂的谷歌搜索...或者,只需在此阅读以下问题就可以了:http://stackoverflow.com/questions/28637/is-datetime-now - 最佳测量方式 - 功能 - 性能 – 2014-09-03 06:21:52
使用['Stopwatch'](http://msdn.microsoft.com/zh-cn/library/system.diagnostics.stopwatch(v = vs。 110).aspx)类而不是'DateTime'。 [相关问题](http://stackoverflow.com/questions/28637/is-datetime-now-the-best-way-to-measure-a-functions-performance)。 – Yuriy 2014-09-03 06:22:38
每次通话需要多长时间?你应该确保你的工作时间很重要 - 这可能意味着拨打数千或数百万次的电话。 – 2014-09-03 06:22:49