[英]Question about DateTime.Now in C#
DateTime startTime = System.DateTime.Now;
//do the search
Graph_SearchBFS BFS = new Graph_SearchBFS(Graph, sourceCell, targetCell);
DateTime endTime = System.DateTime.Now;
How come startTime equals endTime? startTime如何等于endTime? What does that mean? 这意味着什么? By the way my goal is to measure how much time it takes for the graph search. 顺便说一下,我的目标是测量图形搜索需要多少时间。
Thanks 谢谢
It means that the system clock granularity is too large to measure this sort of thing accurately - in other words, calling the constructor happens pretty quickly. 这意味着系统时钟的粒度太大,无法准确地测量此类事件-换句话说,调用构造函数的速度非常快。
However, you can use a class which is more appropriate for precision timing - or at least, it can take advantage of a high-precision clock if one is available: Stopwatch
. 但是,您可以使用更适合精确计时的类-或至少可以使用高精度时钟(如果有的话): Stopwatch
。
Stopwatch sw = Stopwatch.StartNew();
Graph_SearchBFS BFS = new Graph_SearchBFS(Graph, sourceCell, targetCell);
sw.Stop();
Now you can look at the sw.Elapsed
property to see how long it took. 现在,您可以查看sw.Elapsed
属性,以了解花费了多长时间。
Of course, it's entirely possible that it will still be too fast to measure. 当然,这是完全可能的,它仍然是过快来衡量。 Typically for benchmarking you do something enough that you can time over the course of many seconds, rather than a single operation. 通常,为了进行基准测试,您需要做的事情足以使您花费几秒钟的时间而不是一次操作。
As an aside, is your Graph_SearchBFS
constructor actually doing the work? Graph_SearchBFS
,您的Graph_SearchBFS
构造函数实际上是在做这项工作吗? If so, that's a bit of a design smell as well. 如果是这样,那也有点设计味。 Typically I'd expect code more like this: 通常,我希望代码更像这样:
Graph_SearchBFS BFS = new Graph_SearchBFS(Graph);
Stopwatch sw = Stopwatch.StartNew();
BFS.FindCell(sourceCell, targetCell);
sw.Stop();
or something similar. 或类似的东西。 Then you can loop lots of times over the "finding" part: 然后,您可以在“查找”部分中循环很多次:
Graph_SearchBFS BFS = new Graph_SearchBFS(Graph);
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < Iterations; i++)
{
BFS.FindCell(sourceCell, targetCell);
}
sw.Stop();
Probably because your method ran faster than the resolution of the clock. 可能是因为您的方法运行速度快于时钟分辨率。
Use the StopWatch
class instead; 改用StopWatch
类; it's more precise. 更精确。
Could be that the method is very fast. 可能是该方法非常快。 Have you looked at the milliseconds or ticks? 您是否看过毫秒或滴答声?
http://msdn.microsoft.com/en-us/library/system.datetime.millisecond.aspx http://msdn.microsoft.com/zh-CN/library/system.datetime.millisecond.aspx
http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.