Skip to content

Instantly share code, notes, and snippets.

@mattwarren
Last active February 17, 2017 15:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mattwarren/ff9ab4e2f409d9d48d9a38ab8bbe9cad to your computer and use it in GitHub Desktop.
Save mattwarren/ff9ab4e2f409d9d48d9a38ab8bbe9cad to your computer and use it in GitHub Desktop.
[Config(typeof(Config))]
public class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Program>();
}
private class Config : ManualConfig
{
public Config()
{
Add(Job.Clr.WithLaunchCount(1));
Add(new GCDiagnoser());
}
}
// Yeah I know, *mutable* structs are BAD!!!!
public struct ResultAndErrorCode<T>
{
public T Result;
public int ErrorCode;
}
[Benchmark(Baseline = true)]
public ResultAndErrorCode<string> ErrorCodeWithReturnValue()
{
var result = new ResultAndErrorCode<string>();
result.Result = null;
result.ErrorCode = 5;
return result;
}
[Benchmark]
public string ThrowExceptionGetMessage()
{
try
{
throw new InvalidOperationException("Benchmark");
}
catch (InvalidOperationException ioex)
{
// Only get the simple message from the Exception (don't trigger a StackTrace collection)
return ioex.Message;
}
}
[Benchmark]
public string ThrowExceptionGetStackTrace()
{
try
{
throw new InvalidOperationException("Benchmark");
}
catch (InvalidOperationException ioex)
{
// Force collection of a full StackTrace
return ioex.StackTrace;
}
}
}
@mattwarren
Copy link
Author

Results:

image

@mattwarren
Copy link
Author

Updated results (now with a "baseline" comparison of error codes):

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment