Skip to content

Instantly share code, notes, and snippets.

@adamsitnik
Created June 16, 2017 14:43
Show Gist options
  • Save adamsitnik/c4e71c04cc01972feefd24716a6251eb to your computer and use it in GitHub Desktop.
Save adamsitnik/c4e71c04cc01972feefd24716a6251eb to your computer and use it in GitHub Desktop.
hardwareCountersForTwoDifferentRuntimes
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.CsProj;
using System;
using System.Linq;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<Example>();
}
}
public class CustomConfig : ManualConfig
{
public CustomConfig()
{
Add(Job.RyuJitX64.With(Runtime.Core).WithGcConcurrent(true));
Add(Job.RyuJitX64.With(Runtime.Clr).WithGcConcurrent(true));
Add(HardwareCounter.CacheMisses);
Add(MemoryDiagnoser.Default);
}
}
[Config(typeof(CustomConfig))]
public class Example
{
Tuple<int, int>[] arrayOfRef = Enumerable.Repeat(1, 1000000).Select((val, index) => Tuple.Create(val, index)).ToArray();
[Benchmark]
public int IterateReferenceTypes()
{
int item1Sum = 0, item2Sum = 0;
var array = arrayOfRef;
for (int i = 0; i < array.Length; i++)
{
item1Sum += array[i].Item1;
item2Sum += array[i].Item2;
}
return item1Sum + item2Sum;
}
}
}
BenchmarkDotNet=v0.10.8, OS=Windows 10 Redstone 1 (10.0.14393)
Processor=Intel Core i7-6600U CPU 2.60GHz (Skylake), ProcessorCount=4
Frequency=2742189 Hz, Resolution=364.6722 ns, Timer=TSC
  [Host]     : Clr 4.0.30319.42000, 64bit RyuJIT-v4.6.1637.0
  Job-DBWACR : Clr 4.0.30319.42000, 64bit RyuJIT-v4.6.1637.0
  Job-MNPYPV : .NET Core 4.6.25211.01, 64bit RyuJIT

Jit=RyuJit  Platform=X64  Concurrent=True  
Method Runtime Mean Error StdDev Allocated CacheMisses/Op
IterateReferenceTypes Clr 1.657 ms 0.0297 ms 0.0278 ms 0 B 609948
IterateReferenceTypes Core 1.594 ms 0.0313 ms 0.0335 ms 0 B 617856
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp1.1;net46</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net46'">
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.10.8" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.10.8" />
</ItemGroup>
</Project>
@adamsitnik
Copy link
Author

run it from console with: dotnet run -c Release -f net46

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