Created
June 4, 2014 10:06
-
-
Save Ravadre/4c42bad783271c8db19b to your computer and use it in GitHub Desktop.
Obs vs event perf tests
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.Collections.Generic; | |
using System.Linq; | |
using System.Reactive.Subjects; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Reactive.Linq; | |
using Stacks; | |
using System.Threading; | |
using System.Diagnostics; | |
namespace ConsoleApplication23 | |
{ | |
class Foo | |
{ | |
public event Action<int> FooEvent; | |
public event Action<int> FooEventExec; | |
private Subject<int> fooSub; | |
private Subject<int> fooSubExec; | |
public IObservable<int> FooObs { get { return fooSub.AsObservable(); } } | |
public IObservable<int> FooObsExec { get { return fooSubExec.ObserveOn(exec); } } | |
private ActionBlockExecutor exec; | |
public Foo() | |
{ | |
fooSub = new Subject<int>(); | |
fooSubExec = new Subject<int>(); | |
exec = new ActionBlockExecutor(); | |
} | |
public void RunEvents(int count) | |
{ | |
for (int i = 0; i < count; ++i) | |
{ | |
var h = FooEvent; | |
if (h != null) | |
h(i); | |
} | |
} | |
public void RunObs(int count) | |
{ | |
for (int i = 0; i < count; ++i) | |
{ | |
fooSub.OnNext(i); | |
} | |
} | |
public void RunEventsExec(int count) | |
{ | |
for (int i = 0; i < count; ++i) | |
{ | |
var h = FooEventExec; | |
int j = i; | |
if (h != null) | |
exec.Enqueue(() => h(j)); | |
} | |
} | |
public void RunObsExec(int count) | |
{ | |
for (int i = 0; i < count; ++i) | |
{ | |
fooSubExec.OnNext(i); | |
} | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int count = 100000; | |
long expectedSum = (long)count * ((long)count - 1L) / 2L; | |
Foo f = new Foo(); | |
long evSum = 0, obsSum = 0; | |
long evESum = 0, obsESum = 0; | |
f.FooEvent += i => { evSum += i; }; | |
f.FooEventExec += i => { evESum += i; }; | |
f.FooObs.Subscribe(i => { obsSum += i; }); | |
f.FooObsExec.Subscribe(i => { obsESum += i; }); | |
var sw = Stopwatch.StartNew(); | |
f.RunEvents(count); | |
sw.Stop(); | |
Console.WriteLine("Events: " + sw.ElapsedMilliseconds + " ms"); | |
sw.Restart(); | |
f.RunObs(count); | |
sw.Stop(); | |
Console.WriteLine("Obs: " + sw.ElapsedMilliseconds + " ms"); | |
sw.Restart(); | |
f.RunEventsExec(count); | |
while (Volatile.Read(ref evESum) != expectedSum) Thread.Yield(); | |
sw.Stop(); | |
Console.WriteLine("Events (executor): " + sw.ElapsedMilliseconds + " ms"); | |
sw.Restart(); | |
f.RunObsExec(count); | |
while (Volatile.Read(ref obsESum) != expectedSum) Thread.Yield(); | |
sw.Stop(); | |
Console.WriteLine("Obs (executor): " + sw.ElapsedMilliseconds + " ms"); | |
Console.WriteLine(evSum); | |
Console.WriteLine(evESum); | |
Console.WriteLine(obsSum); | |
Console.WriteLine(obsESum); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment