Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HandlingErrorBenchmark
BenchmarkDotNet=v0.13.0, OS=Windows 10.0.18363.1556 (1909/November2019Update/19H2)
Intel Core i7-8665U CPU 1.90GHz (Coffee Lake), 1 CPU, 8 logical and 4 physical cores
.NET SDK=5.0.202
  [Host]     : .NET 5.0.5 (5.0.521.16609), X64 RyuJIT
  DefaultJob : .NET 5.0.5 (5.0.521.16609), X64 RyuJIT

Method Mean Error StdDev Median Gen 0 Gen 1 Gen 2 Allocated
WithException 33,290.94 ns 1,437.640 ns 4,007.565 ns 32,237.84 ns 0.0610 - - 320 B
WithOperationResult 21.38 ns 0.832 ns 2.415 ns 20.59 ns 0.0306 - - 128 B
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using OperationResult;
BenchmarkRunner.Run<HandlingErrorBenchmark>();
[MemoryDiagnoser]
public class HandlingErrorBenchmark
{
private LogicHandler _handler;
[GlobalSetup]
public void GlobalSetup() => _handler = new LogicHandler();
[Benchmark]
public string WithException()
{
try
{
return _handler.ConcatStringsException(default, default);
}
catch (System.Exception e)
{
return e.Message;
}
}
[Benchmark]
public string WithOperationResult()
{
var (success, data, error) = _handler.ConcatStringsResult(default, default);
return (success)
? data
: error.Message;
}
}
public class LogicHandler
{
public string ConcatStringsException(string first, string last)
{
if (string.IsNullOrWhiteSpace(first) || string.IsNullOrWhiteSpace(last))
{
throw new Exception("As strings não podem ser nulas");
}
return $"{first} {last}";
}
public Result<string> ConcatStringsResult(string first, string last)
{
if (string.IsNullOrWhiteSpace(first) || string.IsNullOrWhiteSpace(last))
{
return new Exception("As strings não podem ser nulas");
}
return $"{first} {last}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment