Profiling Example

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (2K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
This is a sample project to show how to add profiling to an application. This was prompted by a question regarding how to speed up the startup of an application (

This profiler implements IDisposable to make it very easy to profile a block of code:

public void SomeMethod()


  using (Profiler profile = new Profiler(System.Reflection.MethodBase.GetCurrentMethod()))

    //-- Code being profiled



You can nest profilers if necessary. You can also just mark a place in code:

//-- in a block of profiled code

profile.AddMark("Label indicating location in code");

The profiler uses a logger to actually track the profile messages. This uses an interface to allow you to create a logger that meets the needs of the specific app. This could log to a file, to a database, to a web service. The sample has three included. Two are form based and one is MessageBox based (used to debug the profiler initially).

The profiling code can be left in source, as it can easily be turned off, add almost no overhead. You can also use command line arguments to control profiling. This is handy if you need to profile a specific client app, but want it off otherwise. You'd use something like:

myapp.exe --EnableProfiling

to turn on profiling. You can also turn it on for only specific types:

myapp.exe --EnableProfiling:MyApp.Tools.Widget,MyApp.UI.WidgetForm

This will enable profiling in any method in either the MyApp.Tools.Widget class or the MyApp.UI.WidgetForm class (of course, you have to add the profiling code as desired to the code itself).

Attachments (77 views, 29.00 KB)

Merge Selected

Merge into selected topic...

Merge into merge target...

Merge into a specific topic ID...

Similar Topics

Reading This Topic