Note
It is recommended to measure performance with Release builds.
public static class Profiler
Public NotInheritable Class Profiler
public ref class Profiler abstract sealed
[<AbstractClassAttribute>]
[<SealedAttribute>]
type Profiler = class end
The Profiler class can be used to place measurement sections into the code. The results can be accessed either directly by the GetMeasurementResults method or when the AutoSaveResults property is , then the results will be dumped into an .XML file into a folder designated by the ProfilerDirectory property.
The profiling can be turned on and off globally by the Enabled property.
using System;
using System.Threading;
using KGySoft.Diagnostics;
class Example
{
static void Main(string[] args)
{
Profiler.Enabled = true; // set false to turn off profiling
Profiler.AutoSaveResults = true; // if true a result .XML file is saved on exit
// put the measurement into a using block. You can specify a category and operation name.
using (Profiler.Measure(nameof(Example), $"{nameof(Main)} total"))
{
for (int i = 0; i < 10; i++)
{
using (Profiler.Measure(nameof(Example), $"{nameof(Main)}/1 iteration"))
{
DoSmallTask();
DoBigTask();
}
}
}
// if Profiler.AutoSaveResults is false you might want to check the results here: Profiler.GetMeasurementResults(nameof(Example));
}
private static void DoSmallTask()
{
using (Profiler.Measure(nameof(Example), nameof(DoSmallTask)))
Console.WriteLine(nameof(DoSmallTask));
}
private static void DoBigTask()
{
using (Profiler.Measure(nameof(Example), nameof(DoBigTask)))
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(10);
DoSmallTask();
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<ProfilerResult>
<item Category = "Example" Operation="Main total" NumberOfCalls="1" FirstCall="00:00:00.5500736" TotalTime="00:00:00.5500736" AverageCallTime="00:00:00.5500736" />
<item Category = "Example" Operation="Main/1 iteration" NumberOfCalls="10" FirstCall="00:00:00.0555439" TotalTime="00:00:00.5500554" AverageCallTime="00:00:00.0550055" />
<item Category = "Example" Operation="DoSmallTask" NumberOfCalls="60" FirstCall="00:00:00.0005378" TotalTime="00:00:00.0124114" AverageCallTime="00:00:00.0002068" />
<item Category = "Example" Operation="DoBigTask" NumberOfCalls="10" FirstCall="00:00:00.0546513" TotalTime="00:00:00.5455339" AverageCallTime="00:00:00.0545533" />
</ProfilerResult>
AutoSaveResults |
Gets or sets whether results are automatically saved into the directory determined by the ProfilerDirectory property.
Default value: |
Enabled |
Gets or sets whether profiling is enabled.
Default value: |
ProfilerDirectory | Gets or sets the output folder of the profiler. When or empty value is assigned, sets the Profiler subdirectory of the executing assembly. |
GetMeasurementResult | Gets a measurement result as an IMeasureItem instance, or , if the measurement result is not found with the given category and operation. |
GetMeasurementResults | Gets the measurement results so far. |
GetMeasurementResults(String) | Gets the measurement results of the given category so far. |
Measure | If Enabled is , starts a profiling measure. Use in using block. |
Reset | Resets the profiler results. Every measurement performed earlier will be lost. |