我如何计算使用 DateTime.Now 调用方法的速度?

How can i calculate the speed the method is call using DateTime.Now?

我将给出方法代码的顶部,因为整个代码很长,在这种情况下不需要。

static int tickCountLength = 1000;
        int[] tickCounts = new int[tickCountLength];
        int numberOfTicks = 0;
        TimeSpan dtn;

        void DoCaptureRenderTarget(Device device, string hook)
        {
            //Environment.TickCount-->array

            tickCounts[numberOfTicks] = Environment.TickCount;
            numberOfTicks++;

            DateTime start1 = DateTime.Now;
            DateTime end1 = DateTime.Now;
            this.DebugMessage("Capture Time Testing " + (end1 - start1).ToString());
            dtn = end1 - start1;
            if (dtn.Seconds > 0)
            {
                string fffff = "fgf";
            }
            if (numberOfTicks >= tickCountLength)
            {

                StreamWriter w = new StreamWriter(@"c:\Milliseconds\TickCount.txt", true);
                foreach (int tick in tickCounts)
                {
                    w.WriteLine("TickCount === > " + tick.ToString());

                }
                w.Close();
                numberOfTicks = 0;
            }

该方法每1ms或20ms调用一次不确定实时。 所以我添加了这部分试图计算:

DateTime start1 = DateTime.Now;
            DateTime end1 = DateTime.Now;
            this.DebugMessage("Capture Time Testing " + (end1 - start1).ToString());
            dtn = end1 - start1;
            if (dtn.Seconds > 0)
            {
                string fffff = "fgf";
            }

但这肯定是错误的,end1 和 start1 之间的时间是 00:00:00:00 我如何以毫秒为单位计算方法每次调用的时间?

也许:每次使用 DateTime.Now.Millisecond 并手动从前一个中减去(通过查看)?不知道该怎么做。

使用StopWatch

示例:

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);// or whatever code you want to calculate elapsed time.
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;

// Format and display the TimeSpan value. 
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
    ts.Hours, ts.Minutes, ts.Seconds,
    ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);

您可以像这样使用 stopwatch

var watch = Stopwatch.StartNew();
// your code for which you want to measure time comes here
watch.Stop();
var elapsedMillisecond = watch.ElapsedMilliseconds;

您也可以使用 Environment.TickCount Property

如果您想知道不推荐 DateTime.Now 的原因和原因,请阅读 John Chapman's blog post 关于 DateTime.Now 精度的好书。

另请阅读这篇有趣的文章:The Darkness Behind DateTime.Now

Stopwatch 就是您所需要的。这里有一个简单的例子:

Stopwatch sw= new Stopwatch();
sw.Start();
// your methods to be measured
sw.Stop();
TimeSpan ts = stopWatch.Elapsed;

//Format 
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
    ts.Hours, ts.Minutes, ts.Seconds,
    ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);

请注意,秒表在多线程异步应用程序中可能存在问题,因为它只能测量一个内核的速度。这个问题可能会导致一些不同步,并给你一些不正确的时间。根据您的应用程序,使用 DateTime.UtcNow 可能会更好,但在单线程上它不如 Stopwatch 精确。 (据我所知)

进一步阅读:

Exact time measurement

Multithreaded stopwatch?

Elapsed ticks msdn

Measuring with stopwatch

好的,首先,您最好使用 Environment.TickCount 而不是 DateTime.Now,尤其是在测量间隔时。

其次,当调用 DateTime.Now(或 Environment.TickCount 时)你实际上得到了 DateTime 对象,因为你调用了 Now get 方法。因此,如果将这些方法一个接一个地放置,您将获得相同的结果。

我不太明白您要测量的是什么,但在 C# 中测量时间的正确方法可能是

int start = Environment.TickCount;
FunctionToMeasure();
int end = Environment.TickCount;
int diffMS = end - start; //This is the measured time of the function.

如果您要测量实际 DateTime.Now 函数所花费的时间,则为 0ms

希望我能帮上忙 :)!

问题是

之间没有调用,也没有逻辑
DateTime start1 = DateTime.Now;

DateTime end1 = DateTime.Now;

所以经过的时间非常小,难以检测也就不足为奇了。把

DateTime start1 = DateTime.Now;

在逻辑之前。

static int tickCountLength = 1000;
    int[] tickCounts = new int[tickCountLength];
    int numberOfTicks = 0;
    TimeSpan dtn;

    void DoCaptureRenderTarget(Device device, string hook)
    {
        //Environment.TickCount-->array
        DateTime start1 = DateTime.Now;
        tickCounts[numberOfTicks] = Environment.TickCount;
        numberOfTicks++;

        DateTime end1 = DateTime.Now;
        this.DebugMessage("Capture Time Testing " + (end1 - start1).ToString());
        dtn = end1 - start1;
        if (dtn.TotalMilliseconds > 0)
        {
            string fffff = "fgf";
        }
        if (numberOfTicks >= tickCountLength)
        {

            StreamWriter w = new StreamWriter(@"c:\Milliseconds\TickCount.txt", true);
            foreach (int tick in tickCounts)
            {
                w.WriteLine("TickCount === > " + tick.ToString());

            }
            w.Close();
            numberOfTicks = 0;
        }

我不知道你的方法中到底有什么逻辑你想及时衡量。