using System;
using System.Collections.Generic;
using System.Linq;
namespace PerfMeasureApp
{
class Program
{
static void Main(string[] args)
{
var queue = new MessageQueue {Clients = GenerateListClients()};
var fastQueue = new MessageQueueSmart {Clients = GenerateDictClients()};
queue.DoStuff(1);
Console.WriteLine("********************************************");
queue.DoStuffLinq(1);
Console.WriteLine("********************************************");
fastQueue.DoStuff(9999999);
Console.WriteLine("********************************************");
fastQueue.DoStuffLinq(9999999);
Console.WriteLine("********************************************");
fastQueue.DoStuffIndex(9999999);
Console.WriteLine("********************************************");
Console.Read();
}
// Define other methods and classes here
public static List<Client> GenerateListClients()
{
var list = new List<Client>();
for (int i = 0; i < 10000000; i++)
{
if (i == 9999999)
list.Add(new Client(1));
else
list.Add(new Client(0));
}
return list;
}
public static Dictionary<int, Client> GenerateDictClients()
{
var dict = new Dictionary<int, Client>();
for (int i = 0; i < 10000000; i++)
{
dict.Add(i, new Client(0));
}
return dict;
}
}
public class MessageQueueSmart
{
private IDictionary<int, Client> _clients;
public IDictionary<int, Client> Clients { set { _clients = value; } }
public void DoStuff(int id)
{
Console.WriteLine("MessageQueueSmart DoStuffTryGet");
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
Client c;
if (_clients.TryGetValue(id, out c))
{
c.SendMessage();
}
stopwatch.Stop();
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
}
public void DoStuffLinq(int id)
{
Console.WriteLine("MessageQueueSmart DoStuffLinq");
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
var c = _clients.FirstOrDefault(x => x.Key == id).Value;
if (c != null)
{
c.SendMessage();
}
stopwatch.Stop();
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
}
public void DoStuffIndex(int id)
{
Console.WriteLine("MessageQueueSmart DoStuffIndex");
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
Client c = _clients[id];
c.SendMessage();
stopwatch.Stop();
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
}
}
public class MessageQueue
{
private List<Client> _clients;
public List<Client> Clients { set { _clients = value; } }
public void DoStuff(int id)
{
Console.WriteLine("MessageQueue DoStuff");
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
foreach (var client in _clients)
{
if (client.Id == id)
{
client.SendMessage();
break;
}
}
stopwatch.Stop();
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
}
public void DoStuffLinq(int id)
{
Console.WriteLine("MessageQueue DoStuffLinq");
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
foreach (var client in _clients.Where(x => x.Id == id))
{
client.SendMessage();
}
stopwatch.Stop();
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
}
}
public class Client
{
public Client(int id)
{
Id = id;
}
public int Id { get; set; }
public void SendMessage()
{
Console.WriteLine("SendMessage called " + Id);
}
}
}