Search code examples
c#stopwatch

How to use the stopwatch to measure the elapsed time between multiple points?


I want to measure the current elapsed time in my code and the time elapsed between multiple code segments. An example:

When i want to get the elapsed time i am doing

static void Main(string[] args)
{
    Stopwatch _stopwatch = new Stopwatch();
    _stopwatch.Start();

    ... do something ...

    _stopwatch.Stop();
    Console.WriteLine("Time elapsed total: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);
}

I can also output the elapsed time during code executing like this

static void Main(string[] args)
{
    Stopwatch _stopwatch = new Stopwatch();
    _stopwatch.Start();

    ... do something ...
    Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);

    ... do something ...
    Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);

    ... do something ...
    Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);

    ... do something ...
    Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);


    // finally
    _stopwatch.Stop();
    Console.WriteLine("Time elapsed total: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);
}

But what i want to is something like this:

Code startet ... | elapsed time 00:02:01:sss | this single segment took 00:02:01:sss

Connected to database ... | elapsed time 00:02:04:sss | this single segment took 00:00:03:sss

Added 40.000 rows ... | elapsed time 00:05:23:sss | this single segment took 00:03:19:sss

So what i want is beside having the total elapsed time and the time stopped for the single sections in code the time for one single section beside the elapsed time.

How could i do this?


Solution

  • I think i solved it. For me it looks maybe some experts can check for "optimization"?

    I am storing the last timespan in an vraiable and subsctracting the last on from the current one:

        private static TimeSpan _pastTimeSpan;
    
        static void Main(string[] args)
        {
            _stopwatch = new Stopwatch();
            _stopwatch.Start();
    
            _pastTimeSpan = _stopwatch.Elapsed;
    
            ... code ...
    
            MyElapsedTime(_stopwatch.Elapsed);
    
            ... code ...
    
            MyElapsedTime(_stopwatch.Elapsed);
    
            ... code ...
    
            MyElapsedTime(_stopwatch.Elapsed);
    
            _stopwatch.Stop();
            Console.WriteLine("Time elapsed total: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);
         }
    
        private static void MyElapsedTime(TimeSpan ts)
        {
            // Get the last TimeSpan
            TimeSpan pastTimeSpan = _pastTimeSpan;
    
            // Update last TimeSpan with current
            _pastTimeSpan = ts;
    
            // Get difference between two
            TimeSpan diffTs = ts.Subtract(pastTimeSpan);
    
            Console.WriteLine(string.Format("Elapsed time {0}:{1} | Segment took {2}:{3}", Math.Floor(ts.TotalMinutes), ts.ToString("ss\\.ff"), Math.Floor(diffTs.TotalMinutes), diffTs.ToString("ss\\.ff")));
        }
    

    And the output will be

    Elapsed time 0:01.70 | Segment took 0:01.70
    Elapsed time 0:01.78 | Segment took 0:00.07