Created
April 2, 2012 17:10
-
-
Save kumpera/2285223 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Diagnostics; | |
| namespace BoxingTest | |
| { | |
| public interface ICriticalNotifyCompletion | |
| { | |
| void UnsafeOnCompleted(Action a); | |
| void UnsafeOnCompleted2(Action a); | |
| } | |
| public interface IAsyncStateMachine | |
| { | |
| void MoveNext(); | |
| } | |
| struct Awaiter : ICriticalNotifyCompletion | |
| { | |
| public void UnsafeOnCompleted(Action a) | |
| { | |
| a(); | |
| } | |
| public void UnsafeOnCompleted2(Action a) | |
| { | |
| for (int i = 0; i < 1000000; ++i) | |
| a(); | |
| } | |
| } | |
| public struct AsyncVoidMethodBuilder | |
| { | |
| public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) | |
| where TAwaiter : ICriticalNotifyCompletion | |
| where TStateMachine : IAsyncStateMachine | |
| { | |
| var action = new Action(stateMachine.MoveNext); | |
| awaiter.UnsafeOnCompleted(action); | |
| } | |
| public void AwaitUnsafeOnCompleted2<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) | |
| where TAwaiter : ICriticalNotifyCompletion | |
| where TStateMachine : IAsyncStateMachine | |
| { | |
| var action = new Action(stateMachine.MoveNext); | |
| awaiter.UnsafeOnCompleted2(action); | |
| } | |
| } | |
| struct Test : IAsyncStateMachine | |
| { | |
| public static int counter; | |
| public void MoveNext() | |
| { | |
| ++counter; | |
| } | |
| public void Entry () | |
| { | |
| var s = new AsyncVoidMethodBuilder(); | |
| var awaiter = new Awaiter(); | |
| s.AwaitUnsafeOnCompleted(ref awaiter, ref this); | |
| } | |
| public void Entry2 () | |
| { | |
| var s = new AsyncVoidMethodBuilder(); | |
| var awaiter = new Awaiter(); | |
| s.AwaitUnsafeOnCompleted2(ref awaiter, ref this); | |
| } | |
| } | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var sw = new Stopwatch(); | |
| sw.Start(); | |
| for (int i = 0; i < 1000000; ++i) | |
| { | |
| new Test().Entry(); | |
| } | |
| sw.Stop(); | |
| Console.WriteLine(sw.ElapsedMilliseconds); | |
| Console.WriteLine("---"); | |
| sw = new Stopwatch(); | |
| sw.Start(); | |
| new Test().Entry2(); | |
| sw.Stop(); | |
| Console.WriteLine(sw.ElapsedMilliseconds); | |
| Console.WriteLine("---"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment