Posts Performance Timer Logging
Post
Cancel

Performance Timer Logging

1
2
3
4
var timer = Stopwatch.StartNew();
var ms = timer.ElapsedMilliseconds;

Just 2 Lines and no ugly indenting of my code. There is really no need to stop the Stopwatch. No resources will be freed if you do, it just stops counting.
1
2
3
4
5
6
7
8
9
10
11
using System.Diagnostics;

var timer = new Stopwatch();
timer.Start();

// do something you want to profile

timer.Stop();
var ms = timer.ElapsedMilliseconds;

// write timing to logs

First, here’s the usage example of the performance timing logging class that will be listed below:

1
2
3
4
using (new PerfTimerLogger("name of code being profiled"))
{
    // do something you want to profile
}

Now, as you can see the above code is MUCH simpler to implement within your code and not very intrusive. Basically, 2 lines of code, instead of 4; and not extra variables to keep track of.

Here’s an implementation of the PerfTimerLogger class that allows it to be used as the above example demonstrates:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System.Diagnostics;

public class PerfTimerLogger : IDisposable
{
    public PerfTimerLogger(string message)
    {
        this._message = message;
        this._timer = new Stopwatch();
        this._timer.Start();
    }

    string _message;
    Stopwatch _timer;

    public void Dispose()
    {
        this._timer.Stop();
        var ms = this._timer.ElapsedMilliseconds;

        // log the performance timing with the Logging library of your choice
        // Example:
        // Logger.Write(
        //     string.Format("{0} - Elapsed Milliseconds: {1}", this._message, ms)
        // );
    }
}

origin - http://www.pipiscrew.com/?p=2875 net-performance-timer-logging

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags