c# 测量各个函数之间的时间

问题描述 投票:0回答:3

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

我现在有了秒表,但我知道我只能通过 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 个秒表

我能做什么?

c# function time stopwatch measure
3个回答
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
投票

你可以让自己成为一个小帮手,为你停止它并返回给你经过的时间:

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()
) 提供的操作 - 但它仅用于测量。


0
投票

这是我过去使用过的一个课程,可能对你有用。它提供了对 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");
© www.soinside.com 2019 - 2024. All rights reserved.