Skip to content

Instantly share code, notes, and snippets.

@kunalspathak
Last active March 4, 2021 00:06
Show Gist options
  • Save kunalspathak/d5babeb3223ec62349f5ee4a4765c9a1 to your computer and use it in GitHub Desktop.
Save kunalspathak/d5babeb3223ec62349f5ee4a4765c9a1 to your computer and use it in GitHub Desktop.
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace MiniBench
{
public class AsyncAccum
{
//[Params(1000)]
//public int Limit { get; set; }
public static IEnumerable<object> Limit => new object[]
{
1,
100,
1000
};
public int singleDefField = 10;
public int multiDefField = 0;
[Benchmark]
[ArgumentsSource(nameof(Limit))]
public void ExceptionHandling(int limit)
{
int lclSingleDefVar = limit - 10;
singleDefField = limit;
int lclMultiDefVar = lclSingleDefVar + singleDefField + multiDefField;
try
{
for (int i = 0; i < limit; i++)
{
lclMultiDefVar += lclSingleDefVar + singleDefField + multiDefField;
multiDefField -= (limit + i);
}
}
finally
{
multiDefField1 += lclSingleDefVar + lclMultiDefVar + singleDefField + multiDefField + limit;
}
}
}
}
/*
Before:
| Method | limit | Mean | Error | StdDev |
|------------------ |------ |-------------:|-----------:|-----------:|
| ExceptionHandling | 1 | 3.302 ns | 0.0384 ns | 0.0341 ns |
| ExceptionHandling | 100 | 162.655 ns | 0.0951 ns | 0.0794 ns |
| ExceptionHandling | 1000 | 1,630.610 ns | 13.9446 ns | 13.0438 ns |
After:
| Method | limit | Mean | Error | StdDev |
|------------------ |------ |-------------:|-----------:|-----------:|
| ExceptionHandling | 1 | 1.640 ns | 0.0244 ns | 0.0216 ns |
| ExceptionHandling | 100 | 150.756 ns | 0.0483 ns | 0.0452 ns |
| ExceptionHandling | 1000 | 1,617.413 ns | 18.9815 ns | 17.7553 ns |
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment