我需要找出两个函数执行相同操作但使用不同算法编写的时间。我需要找到两个中最快的。
下面是我的代码片段
Stopwatch sw = new Stopwatch();
sw.Start();
Console.WriteLine(sample.palindrome()); // algorithm 1
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);//tried sw.elapsed and sw.elapsedticks
sw.Reset(); //tried with and without reset
sw.Start();
Console.WriteLine(sample.isPalindrome()); //algorithm 2
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
从技术上讲,这应该会给出两个算法所需的时间。这使得算法2更快。但是如果我交换两个函数的调用,它会给出不同的时间。例如,如果我先调用algorithm2,然后调用algorithm1,它会说algorithm1更快。
我不知道我做错了什么。
https://stackoverflow.com/questions/27478713
复制相似问题