首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >两个函数之间经过的时间

两个函数之间经过的时间
EN

Stack Overflow用户
提问于 2014-12-15 14:48:55
回答 1查看 1.7K关注 0票数 19

我需要找出两个函数执行相同操作但使用不同算法编写的时间。我需要找到两个中最快的。

下面是我的代码片段

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更快。

我不知道我做错了什么。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27478713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档