-4

I want to measure the time between fucnctions+ get the total time

I now that there is Stopwatch but what I know is that I can only get the time between start and stop with Elapsed , so on that way I can get the total time.

How can I get the time between functions? for ex. :

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();

If I restart the stopwatch between the function I will not have the total time. I don't want to apply more than 1 stopwatch

What can I do?

csc3cc3
  • 11
  • 1
  • What if you had more than one stopwatch? Or if after each function you stored that amount of time in a variable and then added all of the variables up at the end to get the total? – mjwills Dec 28 '17 at 21:59
  • 1
    You don't have to stop the watch to get elapsed ticks. Just get the ticks and store it in a variable when ever you want and do your calculations. – Sam Marion Dec 28 '17 at 22:03
  • if your function like linear like that you can just use DateTime.Now and use math operator to get a timespan – Steve Dec 28 '17 at 22:06
  • Why do you care how many `Stopwatch`es you have? – NetMage Dec 28 '17 at 22:08

3 Answers3

2

You can retrieve the elapsed time from the Stopwatch as you go:

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;
NetMage
  • 26,163
  • 3
  • 34
  • 55
  • var timeF2 = s.Elapsed; give the time from s.Start(); until var timeF2 = s.Elapsed, but I want to know the time that Function2 takes – csc3cc3 Dec 29 '17 at 03:19
0

You could make yourself a small helper that stops it for you and returns you the elapsed times:

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()));      
    }
}

Output:

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

The StopFunc takes an Action which you supply by a lambda ( () => YourFuncToCall() ) - it is just for measurements though.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
-1

Here is a class I've used in the past that may be useful to you. It provides a simple abstraction over the Stopwatch class which allows you to keep your code as free as possible of boiler plate code needed to do the timing. This makes it easier to read and easier to remove at a later stage.

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);
    }
}

You could use it in your scenario as follows:

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");
}
pmcilreavy
  • 3,076
  • 2
  • 28
  • 37
  • 2
    Your class provides literally zero functionality not provided by the `StopWatch` class. – Servy Dec 28 '17 at 22:32
  • 1
    @Servy Rather than having to define a bunch of intermediate vars or wrapping each function call in an Action, I find it provides a cleaner and less intrusive way of achieving the desired result the questioner is seeking without having to pollute the actual code under test with a bunch of boiler plate stuff. – pmcilreavy Dec 28 '17 at 22:41