Skip to content

Instantly share code, notes, and snippets.

@brotatotes
Created May 19, 2021 03:14
Show Gist options
  • Save brotatotes/a8e06b408313f041fcea60891afc820a to your computer and use it in GitHub Desktop.
Save brotatotes/a8e06b408313f041fcea60891afc820a to your computer and use it in GitHub Desktop.
ManualResetEventSlim Benchmark
using System;
using System.Threading;
using System.Diagnostics;
public class Program
{
public static void Main()
{
long warmupTimes = 100000000;
long testTimes = warmupTimes * 10;
// Warmup
CheckSet(warmupTimes);
CheckSet(testTimes);
JustSet(warmupTimes);
JustSet(testTimes);
Console.WriteLine("Hello World");
}
private static void CheckSet(long times)
{
Console.Write("CheckSet " + times + ": ");
ManualResetEventSlim e = new ManualResetEventSlim(false);
Stopwatch s = new Stopwatch();
s.Start();
for (long i = 0; i < times; i++)
{
if (!e.IsSet)
{
e.Set();
}
}
Console.WriteLine(s.ElapsedMilliseconds / 1000.0);
}
private static void JustSet(long times)
{
Console.Write("JustSet " + times + ": ");
ManualResetEventSlim e = new ManualResetEventSlim(false);
Stopwatch s = new Stopwatch();
s.Start();
for (long i = 0; i < times; i++)
{
e.Set();
}
Console.WriteLine(s.ElapsedMilliseconds / 1000.0);
}
}
@brotatotes
Copy link
Author

image

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