Skip to content

Instantly share code, notes, and snippets.

@glikoz
Last active July 2, 2020 17:46
Show Gist options
  • Save glikoz/bd767af231e7356bf97bd65ca76024a7 to your computer and use it in GitHub Desktop.
Save glikoz/bd767af231e7356bf97bd65ca76024a7 to your computer and use it in GitHub Desktop.
class Program
{
static ActorSystem System;
static int loop = 1000;
static int maxParallel=100;//For Parallel Foreach
static int taskload = 50;
static void Main(string[] args)
{
System = ActorSystem.Create("system");
foreach (var item in Enumerable.Range(0, loop))
{
System.ActorOf(Props.Create(() => new Actor()), $"{Guid.NewGuid()}");
}
var selection = System.ActorSelection("/user/*");
/// Akka.net Actors
var sw = Stopwatch.StartNew();
selection.Ask(1).Wait();
//var y = selection.Ask(1).Result;
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
var actorList = new List<FakeActor>();
foreach (var item in Enumerable.Range(0, loop))
{
actorList.Add(new FakeActor());
}
/// Standard Foreach Clear Loser
//sw.Restart();
//foreach (var fakeActor in actorList)
//{
// fakeActor.Ask(1);
//}
//sw.Stop();
//Console.WriteLine(sw.ElapsedMilliseconds);
///Parallel Foreach with MaxParallel options
sw.Restart();
Parallel.ForEach(actorList, new ParallelOptions { MaxDegreeOfParallelism = maxParallel }, actor => actor.Ask(1));
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
///Parallel Foreach without MaxParallel options
sw.Restart();
Parallel.ForEach(actorList, actor => actor.Ask(1));
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
Console.ReadLine();
}
public class FakeActor
{
public int Ask(int p)
{
Thread.Sleep(taskload);
return p * 2;
}
}
public class Actor : ReceiveActor
{
public Actor()
{
Receive<int>(p => { Thread.Sleep(taskload); Sender.Tell(p * 2); });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment