We use the Stopwatch class from the System.Diagnostics namespace to measure the time that each query takes.
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Diagnostics; class Program//from ww w . j a v a 2 s. co m { static void Main(string[] args) { // create the sequential number range IEnumerable<int> numbers1 = Enumerable.Range(0, Int32.MaxValue); // start the stop watch Stopwatch sw = Stopwatch.StartNew(); // perform the LINQ query int sum1 = (from n in numbers1 where n % 2 == 0 select n).Count(); // write out the seqential result Console.WriteLine("Seqential result: {0}", sum1); // write out how long the sequential execution took Console.WriteLine("Sequential time: {0} ms", sw.ElapsedMilliseconds); // create the parallel number range IEnumerable<int> numbers2 = ParallelEnumerable.Range(0, Int32.MaxValue); // Restart the stopwatch sw.Restart(); // perform the Parallel LINQ query int sum2 = (from n in numbers2.AsParallel() where n % 2 == 0 select n).Count(); // write the parallel result Console.WriteLine("Parallel result: {0}", sum2); // write out how long the parallel execution took Console.WriteLine("Parallel time: {0} ms", sw.ElapsedMilliseconds); } }