Skip to content

Instantly share code, notes, and snippets.

@rogeralsing
Last active March 7, 2017 21:22
Show Gist options
  • Select an option

  • Save rogeralsing/0b7a923786e0dc6e54c5c82955133c75 to your computer and use it in GitHub Desktop.

Select an option

Save rogeralsing/0b7a923786e0dc6e54c5c82955133c75 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using Akka.Actor;
namespace SpawnTest
{
internal class Request
{
public long Div;
public long Num;
public long Size;
}
internal class Actor : UntypedActor
{
private long replies;
private IActorRef replyTo;
private long sum = 0;
protected override void OnReceive(object message)
{
if (message is long)
{
var l = (long) message;
sum += l;
replies--;
if (replies == 0)
{
replyTo.Tell(sum);
}
}
if (message is Request)
{
var msg = (Request) message;
if (msg.Size == 1)
{
Sender.Tell(msg.Num);
Context.Stop(Context.Self);
return;
}
replyTo = Sender;
replies = msg.Div;
for (var i = 0; i < msg.Div; i++)
{
var child = Context.ActorOf<Actor>();
child.Tell(new Request
{
Num = msg.Num + i*(msg.Size/msg.Div),
Size = msg.Size/msg.Div,
Div = msg.Div
}, Self);
}
}
}
}
internal class Program
{
private static void Main(string[] args)
{
using (var sys = ActorSystem.Create("aaa"))
{
var actor = sys.ActorOf<Actor>();
var sw = Stopwatch.StartNew();
var res = actor.Ask<long>(new Request
{
Div = 10,
Num = 0,
Size = 1000000
}).Result;
sw.Stop();
Console.WriteLine(res);
Console.WriteLine(sw.Elapsed);
Console.ReadLine();
}
}
}
}
@rogeralsing
Copy link
Author

This is the benchmark. https://github.com/atemerev/skynet

Spawn one root actor, spin up 10 children, let them spin up 10 children each, repeatedly until size == 1
Then send back the result all the way up.

Scala Akka does this in roughly ~2 sec.
Erlang in 1-3 depending on compiler.

Tasks/Fibers/Promises are a lot faster..

Akka.NET does this in 15+ sec.

result: 499999500000
elapsed: 00:00:15.8041196

@rogeralsing
Copy link
Author

Updated code, there was no code to stop the children upon completion. (line 38)

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