Skip to content

Instantly share code, notes, and snippets.

@wilson0x4d
Last active April 8, 2021 14:53
Show Gist options
  • Save wilson0x4d/7b30c3913e74adf4ad99b09163a57a1f to your computer and use it in GitHub Desktop.
Save wilson0x4d/7b30c3913e74adf4ad99b09163a57a1f to your computer and use it in GitHub Desktop.
Benchmark of `StackTrace`, `StackFrame` and `CallerMemberNameAttribute`.
BenchmarkDotNet=v0.10.13, OS=Windows 10 Redstone 3 [1709, Fall Creators Update] (10.0.16299.248)
Intel Core i7-7700K CPU 4.20GHz (Kaby Lake), 1 CPU, 8 logical cores and 4 physical cores
.NET Core SDK=2.1.4
[Host] : .NET Core 2.0.5 (CoreCLR 4.6.26020.03, CoreFX 4.6.26018.01), 64bit RyuJIT
DefaultJob : .NET Core 2.0.5 (CoreCLR 4.6.26020.03, CoreFX 4.6.26018.01), 64bit RyuJIT
Method | Mean | Error | StdDev | Median |
---------------------------- |---------:|----------:|----------:|---------:|
Diagnostics_StackTrace_Cost | 34.34 us | 0.2898 us | 0.2711 us | 34.26 us |
Diagnostics_StackFrame_Cost | 32.91 us | 0.6506 us | 1.2535 us | 33.04 us |
Diagnostics_CallerName_Cost | 22.98 us | 0.4569 us | 0.6698 us | 22.63 us |
public class DiagnosticsBenchmarks
{
[Benchmark]
public void Diagnostics_StackTrace_Cost()
{
Diagnostics_StackTrace_Cost_INNER();
}
public void Diagnostics_StackTrace_Cost_INNER()
{
var stackTrace = new StackTrace();
Trace.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
}
[Benchmark]
public void Diagnostics_StackFrame_Cost()
{
Diagnostics_StackFrame_Cost_INNER();
}
public void Diagnostics_StackFrame_Cost_INNER()
{
Trace.WriteLine(new StackFrame(1).GetMethod().Name);
}
[Benchmark]
public void Diagnostics_CallerName_Cost()
{
Diagnostics_CallerName_Cost_INNER("test");
}
public void Diagnostics_CallerName_Cost_INNER(
[CallerMemberName] string callerName = "")
{
Trace.WriteLine(callerName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment