-
-
Save MonkAlex/3ee81313728b3b7227a8a223f64973c3 to your computer and use it in GitHub Desktop.
habr 803273 example
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 BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using Threading; | |
public static class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<Bench>(); | |
} | |
[ThreadingDiagnoser] | |
public class Bench | |
{ | |
[Params(10, 100, 1000, 10_000)] | |
public int Repeat; | |
[Benchmark] | |
public void Lock() | |
{ | |
var a = new Lock(); | |
for (int i = 0; i < Repeat; i++) | |
{ | |
a.SomeMethod(); | |
} | |
a.Assert(Repeat); | |
} | |
[Benchmark] | |
public void QueueExample() | |
{ | |
var a = new SerialQueueExample(); | |
for (int i = 0; i < Repeat; i++) | |
{ | |
a.SomeMethod(); | |
} | |
a.Assert(Repeat); | |
} | |
} | |
class Lock | |
{ | |
readonly object locker = new object(); | |
private int counter = 0; | |
public void SomeMethod() | |
{ | |
lock (locker) | |
{ | |
counter++; | |
} | |
} | |
public void Assert(int input) | |
{ | |
if (input != counter) | |
{ | |
throw new InvalidOperationException($"Input {input}, counter {counter}"); | |
} | |
} | |
} | |
class SerialQueueExample | |
{ | |
readonly SerialQueue queue = new SerialQueue(); | |
private int counter = 0; | |
public void SomeMethod() | |
{ | |
queue.DispatchAsync(() => { | |
counter++; | |
}); | |
} | |
public void Assert(int input) | |
{ | |
if (input != counter) | |
{ | |
throw new InvalidOperationException($"Input {input}, counter {counter}"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Выше ошибка, вместо DispatchAsync нужно использовать DispatchSync
Тогда производительность на линейном чтении отвратительная: