Click here to Skip to main content
11,411,241 members (65,259 online)
Click here to Skip to main content
Add your own
alternative version

LINQ Performance Test: My First Visual Studio 2008 Project

, 21 Dec 2007 CPOL
A sample Visual Studio 2008 project that compares the performance of LINQ to simpler loops
linqtest_src.zip
LinqTest1.vsmdi
LinqTest.suo
LinqTest.vsmdi
LocalTestRun.testrunconfig
Properties
TestProject1
obj
Debug
TempPE
Release
LinqTest_Accessor.dll
LinqTest_Accessor.pdb
ResolveAssemblyReference.cache
TempPE
TestProject1.dll
TestProject1.pdb
Properties
Test References
LinqTest.accessor
using System;
using System.Linq;
using Win32;
using System.IO;
using System.Text;

namespace LinqTest
{
    class Program
    {
        //use system timer - created by Daniel Strigl
        private static HiPerfTimer pt = new HiPerfTimer();
        //the array
        private static int [] theArray;
        //how many iterations used to measure average
        private static int numIterations = 1000;
        private delegate int func();

        static void Main(string[] args)
        {
            //use of StringBuilder to avoid file noise suggested by kckn4fun on CodeProject 12/14/2007
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Elements\tFor loop\tForEach Loop\tLinq Loop");
            
            for (int i = 0; i < 5; i++)
            {
                int numElements = 1000 * (int)Math.Pow(10, i);
                FillArray(numElements);
                sb.AppendLine(string.Format("{0:#,#}\t{1:0.0000000000}\t{2:0.0000000000}\t{3:0.0000000000}", numElements, GetAverage(GetOdd), GetAverage(GetOddForEach), GetAverage(GetOddLinq)));
            }
            Console.ReadLine();
            WriteFile(sb.ToString());
        }

        private static void WriteFile(string results)
        {
            //Console.WriteLine(results);
            try
            {
                StreamWriter file = new StreamWriter("results.txt");
                file.WriteLine(results);
                file.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("File OperationCanceledException failed: " + ex.Message);
            }
        }

        private static void FillArray(int n)
        {
            theArray = new int[n];
            for (int i = 0; i < n; theArray[i] = i++) ;
        }

        private static double GetAverage(func f)
        {
            double averageDuration = 0.0;
            for (int i = 0; i < numIterations; i++)
            {
                pt.Start();
                int odd = f();
                pt.Stop();
                averageDuration += pt.Duration;
            }
            averageDuration /= numIterations;
            return averageDuration;
        }

        private static int GetOdd()
        {
            int counter = 0;
            for(int n = 0; n < theArray.Length; n++)
            {
                if (theArray[n] % 2 == 1)
                {
                    counter++;
                }
            }
            return counter;
        }

        private static int GetOddForEach()
        {
            int counter = 0;
            foreach (int n in theArray)
            {
                if (n % 2 == 1)
                {
                    counter++;
                }
            }
            return counter;
        }

        private static int GetOddLinq()
        {
            //var odd = from n in theArray
            //          where n % 2 == 1
            //          select n;

            //return odd.Count();

            //per Dennis Dollfus's suggestion on CodeProject 12/14/2007
            int oddNumbers = theArray.Count(n => n % 2 == 1);
            return oddNumbers;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Guy Vider
Product Manager
United States United States
Currently, I manage the West Coast Professional Services team for a large company. Part of my job includes implementing solutions and developing "glue" applications.

I love RAD (Rapid Application Development) - specify a problem, come up with the solution, code it - and change later. This is where coding comes closest to art. Just let it flow...

If you want more biographical items, look at my LinkedIn profile at http://www.linkedin.com/in/gvider and if you'd like to see my opinion on other tech-related subjects, read my blog at http://www.guyvider.com.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.5 | Last Updated 22 Dec 2007
Article Copyright 2007 by Guy Vider
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid