c#测量各种功能之间的时间

时间:2017-12-28 21:57:14

标签: c# function time stopwatch measure

我想测量功能之间的时间+获得总时间

我现在有秒表,但我知道的是我只能通过Elapsed获得开始和停止之间的时间,所以就这样我可以得到总时间。

如何在功能之间获得时间?对于前:

Stopwatch s= new Stopwatch();
s.Start();
Function1();
//here I want get the time of function1
Function2();
//here I want get the time of function2
Function3();
//here I want get the time of function3

//here I want get the time of total time
s.Stop();

如果我在功能之间重新启动秒表,我将没有总时间。 我不想申请超过1个秒表

我该怎么办?

3 个答案:

答案 0 :(得分:2)

您可以随时从Stopwatch检索已用时间:

Stopwatch s= new Stopwatch();
s.Start();
Function1();
var timeF1 = s.Elapsed;
Function2();
var timeF2 = s.Elapsed-timeF1;
Function3();
var timeF3 = s.Elapsed-timeF2-timeF1;

s.Stop();
//here I want get the time of total time
var timeTotal = s.Elapsed;

答案 1 :(得分:0)

你可以让自己成为一个帮助你停下来的小帮手,然后回过去的时间:

using System;
using System.Threading;
using System.Diagnostics;

public class Program
{
    // measures a given lambdas run time - you can call any
    // other function by  StopFunc( () => ......... ) and supply 
    // your function call instead of ........
    public static TimeSpan StopFunc(Action act)
    {
        var watch = Stopwatch.StartNew();

        act?.Invoke(); // call the function

        watch.Stop();

        return watch.Elapsed;
    } 

    static void Fast() { Thread.Sleep(100); } 
    static void Slow() { Thread.Sleep(2000); }

    public static void Main()
    {
        // this calls the "to be measured function"  
        // via lambda and prints the time needed
        Console.WriteLine("Slow took: " + StopFunc( () => Slow()));
        Console.WriteLine("Fast took: " + StopFunc( () => Fast()));      
    }
}

输出:

Slow took: 00:00:02.0024322
Fast took: 00:00:00.1099702

StopFunc接受一个由lambda(() => YourFuncToCall())提供的Action - 它仅用于测量。

答案 2 :(得分:-2)

这是我过去使用过的一个对你有用的课程。它提供了一个简单的Stopwatch类抽象,它允许您尽可能地保持代码免费进行定时所需的样板代码。这样可以更容易阅读,并在以后更容易删除。

public class StopwatchWrapper : IDisposable
{
    private bool disposed = false;
    private Stopwatch _overallStopwatch;
    private List<long> _increments;

    public StopwatchWrapper()
    {
        _overallStopwatch = Stopwatch.StartNew();
        _increments = new List<long>();
    }

    public void Reset()
    {
        _increments.Clear();
        _overallStopwatch.Restart();
    }

    public long ElapsedMilliseconds
    {
        get
        {
            _overallStopwatch.Stop();
            var elapsed = _overallStopwatch.ElapsedMilliseconds;
            _increments.Add(elapsed);
            _overallStopwatch.Start();

            return elapsed;
        }
    }

    public long OverallMilliseconds
    {
        get
        {
            return _increments.Sum();
        }
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!disposed)
        {
            if (disposing)
            {
                if (_overallStopwatch != null)
                {
                    _overallStopwatch.Stop();
                }
            }

            disposed = true;
        }
    }

    public void Dispose()
    {
        Dispose(true);
    }
}

您可以在您的方案中使用它,如下所示:

using (var sw = new StopwatchWrapper())
{
    Function1();
    //here I want get the time of function1
    Console.WriteLine($"{sw.ElapsedMilliseconds}ms");

    Function2();
    //here I want get the time of function2
    Console.WriteLine($"{sw.ElapsedMilliseconds}ms");

    Function3();
    //here I want get the time of function3
    Console.WriteLine($"{sw.ElapsedMilliseconds}ms");

    //here I want get the time of total time
    Console.WriteLine($"{sw.OverallMilliseconds}ms");
}