时间流逝计算(以毫秒为单位)C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13589853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Time elapse computation in milliseconds C#
提问by Sebi
I need to time the execution of a code sequence written in C#. Using DateTime.Now I get incorrect values for the millisecond field. For example:
我需要对用 C# 编写的代码序列的执行计时。使用 DateTime.Now 我得到的毫秒字段值不正确。例如:
int start_time, elapsed_time;
start_time = DateTime.Now.Millisecond;
for(int i = 0; i < N_ITER; i++) {
// cpu intensive sequence
}
elapsed_time = DateTime.Now.Millisecond - start_time;
elapsed_time gives negative values.
elapsed_time 给出负值。
How may I replace DateTime in order to obtain the actual value of the elapsed time?
如何替换 DateTime 以获得经过时间的实际值?
采纳答案by RavingDev
using System.Diagnostics;
//...
var stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < N_ITER; i++) {
// cpu intensive sequence
}
stopwatch.Stop();
elapsed_time = stopwatch.ElapsedMilliseconds;
回答by bonCodigo
Answer EDITED based on comments
根据评论回答编辑
This answer is only trying to count the total elapsed Milliseconds between two times, where the times are derived directly from DateTime.Now. As per the conversation, it's understood that DateTime.Nowis vulnerable to outside influences. Hence the best solution would be to use the Stopwatchclass. Here's a link that better explains (IMO) and discusses the performance between DateTimeNow, DateTime.Ticks, StopWatch.
这个答案只是试图计算两次之间经过的总毫秒数,其中时间直接来自DateTime.Now。根据对话,据了解,这DateTime.Now很容易受到外部影响。因此,最好的解决方案是使用Stopwatch该类。这是一个链接,可以更好地解释 (IMO) 并讨论DateTimeNow、DateTime.Ticks、StopWatch 之间的性能。
Original Answer
原答案
The way you cast it into a int is the issue. You need better casting and extra elements :) This may looks simple compared to an efficient timer. But it works:
您将其转换为 int 的方式是问题所在。您需要更好的投射和额外的元素:) 与高效的计时器相比,这看起来很简单。但它有效:
DateTime startTime, endTime;
startTime = DateTime.Now;
//do your work
endTime = DateTime.Now;
Double elapsedMillisecs = ((TimeSpan)(endTime - startTime)).TotalMilliseconds;
There is a referenceon the web, you may want to check out as well.
网上有参考,你也可以看看。
回答by tmesser
You're looking for the Stopwatchclass. It is specifically designed to bring back high-accuracy time measurements.
您正在寻找Stopwatch类。它专为恢复高精度时间测量而设计。
var stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < N_ITER; i++)
{
// cpu intensive sequence
}
stopwatch.Stop();
var elapsed = stopwatch.ElapsedMilliseconds;
回答by StuartLC
DateTime.Millisecondjust returns the millisecond fraction of the second, from 0-999. You would need to take the rest of the datetime into consideration when doing timings.
DateTime.Millisecond只返回秒的毫秒部分,从 0 到 999。在进行计时时,您需要考虑其余的日期时间。
However, you should look at using the StopWatchclass for these kinds of performance timings.
但是,您应该考虑将StopWatch类用于这些类型的性能计时。
回答by Constantin Cabiniuc
Here is what I used to obtain the time for a simple computation:
这是我用来获取时间进行简单计算的内容:
class Program
{
static void Main(string[] args)
{
Decimal p = 0.00001m;
Decimal i = 0m;
DateTime start = new DateTime();
DateTime stop = new DateTime();
for (i = p; i <= 5; i = i + p)
{
Console.WriteLine("result is: " + i);
if (i==p) start = DateTime.Now;
if (i==5) stop = DateTime.Now;
}
Console.WriteLine("Time to compute: " + (stop-start));
}
}
回答by Clive
This works for me:
这对我有用:
var lapsedTime = DateTime.Now.Subtract(beginTime).TotalMilliseconds;
回答by Valentin Petkov
Stopwatch there are examples in the URL
https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch?view=netframework-4.8
秒表在 URL https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch?view=netframework-4.8 中有示例
using System.Diagnostics;
//...
Stopwatch watch = new Stopwatch();
watch.Start();
// here the complex program.
//...
watch.Stop();
TimeSpan timeSpan = watch.Elapsed;
Console.WriteLine("Time: {0}h {1}m {2}s {3}ms", timeSpan.Hours, timeSpan.Minutes,
timeSpan.Seconds, timeSpan.Milliseconds);

