Last active
January 1, 2016 20:32
-
-
Save ayende/3cf665c613e90d9320f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using BenchmarkDotNet; | |
namespace ConsoleApplication5 | |
{ | |
public class Program | |
{ | |
ConcurrentDictionary<int, ConcurrentStack<int>> _freeSegments = new ConcurrentDictionary<int, ConcurrentStack<int>>(); | |
private readonly Func<int, ConcurrentStack<int>> _valueFactory = i => new ConcurrentStack<int>(); | |
private static readonly Func<int, ConcurrentStack<int>> _staticValueFactory = i => new ConcurrentStack<int>(); | |
static void Main(string[] args) | |
{ | |
new BenchmarkRunner().Run<Program>(); | |
} | |
[Benchmark] | |
[OperationsPerInvoke(10)] | |
public void CompilerDoesWork() | |
{ | |
for (int i = 0; i < 10; i++) | |
{ | |
var q = _freeSegments.GetOrAdd(2, size => new ConcurrentStack<int>()); | |
q.Push(3); | |
if (q.Count > 1024) | |
q.Clear(); | |
} | |
} | |
[Benchmark] | |
[OperationsPerInvoke(10)] | |
public void ReadOnlyField() | |
{ | |
for (int i = 0; i < 10; i++) | |
{ | |
var q = _freeSegments.GetOrAdd(2, _valueFactory); | |
q.Push(3); | |
if (q.Count > 1024) | |
q.Clear(); | |
} | |
} | |
[Benchmark] | |
[OperationsPerInvoke(10)] | |
public void ReadOnlyFieldStatic() | |
{ | |
for (int i = 0; i < 10; i++) | |
{ | |
var q = _freeSegments.GetOrAdd(2, _staticValueFactory); | |
q.Push(3); | |
if (q.Count > 1024) | |
q.Clear(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BenchmarkDotNet=v0.8.0.0 | |
OS=Microsoft Windows NT 6.2.9200.0 | |
Processor=Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz, ProcessorCount=8 | |
HostCLR=MS.NET 4.0.30319.42000, Arch=32-bit | |
Type=Program Mode=Throughput Platform=HostPlatform Jit=HostJit .NET=HostFramework toolchain=Classic Runtime=Clr Warmup=5 Target=10 | |
Method | AvrTime | StdDev | op/s | | |
-------------------- |------------ |---------- |------------- | | |
CompilerDoesWork | 695.5009 ns | 4.1586 ns | 1,437,862.04 | | |
ReadOnlyField | 679.8743 ns | 5.9000 ns | 1,470,966.85 | | |
ReadOnlyFieldStatic | 686.0820 ns | 7.5863 ns | 1,457,724.95 | | |
BenchmarkDotNet=v0.8.0.0 | |
OS=Microsoft Windows NT 6.2.9200.0 | |
Processor=Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz, ProcessorCount=8 | |
HostCLR=MS.NET 4.0.30319.42000, Arch=64-bit [RyuJIT] | |
Type=Program Mode=Throughput Platform=HostPlatform Jit=HostJit .NET=HostFramework toolchain=Classic Runtime=Clr Warmup=5 Target=10 | |
Method | AvrTime | StdDev | op/s | | |
-------------------- |------------ |----------- |------------- | | |
CompilerDoesWork | 712.0287 ns | 8.1491 ns | 1,404,617.41 | | |
ReadOnlyField | 829.7725 ns | 75.5855 ns | 1,214,984.00 | | |
ReadOnlyFieldStatic | 798.9948 ns | 70.2898 ns | 1,260,914.23 | | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment