Created
August 25, 2016 12:42
-
-
Save benaadams/a8de9032ae5f6ff89370842fb495d5fa 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
public class Program | |
{ | |
private const int iters = 1000 * 1000; | |
public static void Main(string[] args) | |
{ | |
bool lockTaken = false; | |
var s = new SpinLock(false); | |
s.Enter(ref lockTaken); | |
if (!lockTaken) | |
{ | |
Console.WriteLine("*** Error couldn't take lock!"); | |
} | |
var sw = Stopwatch.StartNew(); | |
for (var i = 0; i < 1000; i++) | |
{ | |
lockTaken = false; | |
s.TryEnter(0, ref lockTaken); | |
} | |
sw.Stop(); | |
GC.Collect(); | |
sw.Restart(); | |
for (var i = 0; i < iters; i++) | |
{ | |
lockTaken = false; | |
s.TryEnter(0, ref lockTaken); | |
} | |
sw.Stop(); | |
Console.WriteLine(sw.Elapsed.TotalMilliseconds); | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment