X

C# Perf improvement data types or using Linq is faster?

linq

Recently I was asked a simple code to improve the performance, and at first I couldn’t see it but as you know your brain starts working when you aren’t thinking of the problem anymore. (It happen to me when I was driving)

Anyway the problem on hand was there is a for loop and when there are 10000 clients it performs bad.
Something along the line of.

List _client;

public void SendMessage(int id)
{
  foreach(var c in _client)
  {
     if(c.Id == id)
     { 
       c.SendMessage();
       break;
     }
  }

So I went and wrote some code to see the perf using Linq and at the end using a Dictionary rather, since a dictionary is way faster in lookup, O(1) in this case.

Here is the code using a List and Linq together

 foreach (var client in _clients.Where(x => x.Id == id))
{
                client.SendMessage();
}

and if we changed the datatype to Dictionary this was the code used

//using tryget
if (_clients.TryGetValue(id, out c))
{
  c.SendMessage();
}

//using linq
 var c = _clients.FirstOrDefault(x => x.Key == id).Value;

if (c != null)
{
    c.SendMessage();
}
//using just an index
 Client c = _clients[id];

c.SendMessage();

Here is the end result of using it on an i7 8G of ram machine.

And the results show that using an index as a dictionary is definitely faster, but one also has to look at the number of data.
Where there are only few data entry using linq is quite insignificant in performance but where there are millions of data, it really shows that it slows down.

Here is the rest of the code.

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 GenerateListClients()
        {
            var list = new List();

            for (int i = 0; i  GenerateDictClients()
        {
            var dict = new Dictionary();

            for (int i = 0; i  _clients;

        public IDictionary 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 _clients;

        public List 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);
        }


    }
}
Categories: .NET Perf Testing
Taswar Bhatti:

View Comments (3)

  • I don't think Linq is an issue when processing millions of records if the records are being processed over a period of hours or days or years. It depends on long you need it to take. Linq makes code easy to read, maintain and write. Sometimes it's ok to make that trade off. Only avoid Linq when it's not fast enough.

    • I definitely agree with you, and I am a strong believer/user of Linq. I dont think one should avoid it but should know the trade-offs of using any technologies :)

      Thanks for the comment.

  • Thanks for this. I have a dataset that can contain upwards to 10k items at a time, and I needed a good way to search without LinQ. It's amazing how much you forget from college Data Structs course after a few years.

Related Post