Note that there are some explanatory texts on larger screens.

plurals
  1. POPerformance Linq
    primarykey
    data
    text
    <p>Today I tested the performance impacts of Linq and PLinq querys. Therefore I used the article on <a href="http://msdn.microsoft.com/en-gb/library/dd554944.aspx" rel="nofollow">msdn How to: Measure PLINQ Query Performance</a>. </p> <pre><code>void Main() { var source = Enumerable.Range(0, 600000000); System.Diagnostics.Stopwatch sw; var queryToMeasure1 = from num in source where num % 3 == 0 select Math.Sqrt(num); var queryToMeasure2 = from num in source.AsParallel() where num % 3 == 0 select Math.Sqrt(num); long freq = Stopwatch.Frequency; Console.WriteLine("Timer frequency in ticks per second = {0}", freq); Console.WriteLine("Measuring 1"); sw = System.Diagnostics.Stopwatch.StartNew(); foreach (var n in queryToMeasure1) { } Console.WriteLine("Total ticks: {0} - Elapsed time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds); Console.WriteLine("Measuring 2"); sw = System.Diagnostics.Stopwatch.StartNew(); foreach (var n in queryToMeasure2) { } Console.WriteLine("Total ticks: {0} - Elapsed time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds); Console.WriteLine("Measuring 3"); sw = System.Diagnostics.Stopwatch.StartNew(); System.Threading.Tasks.Parallel.ForEach(queryToMeasure1, n =&gt; {}); Console.WriteLine("Total ticks: {0} - Elapsed time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds); Console.WriteLine("Measuring 4"); sw = System.Diagnostics.Stopwatch.StartNew(); System.Threading.Tasks.Parallel.ForEach(queryToMeasure2, n =&gt; {}); Console.WriteLine("Total ticks: {0} - Elapsed time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds); Console.WriteLine("Measuring 5"); sw = System.Diagnostics.Stopwatch.StartNew(); queryToMeasure2.ForAll(n =&gt; {}); Console.WriteLine("Total ticks: {0} - Elapsed time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);); } </code></pre> <p>Test environment: LinqPad4 on Win7 Enterprise, 64bit, 8GB RAM, I7-2600 (8cores)</p> <p>I figured out, and can't explain, why the query on one core (Measurement 1) are faster than the paralleled queries. Do I have to add more select delegates to get benefit from paralleled tasks?</p> <p>But now the results:</p> <p>1.Run: with an enumerable range of 60000:</p> <pre><code>Timer frequency in ticks per second = 3312851 Measuring 1 Total ticks: 3525 - Elapsed time: 1 ms Measuring 2 Total ticks: 15802 - Elapsed time: 4 ms Measuring 3 Total ticks: 5940 - Elapsed time: 1 ms Measuring 4 Total ticks: 26862 - Elapsed time: 8 ms Measuring 5 Total ticks: 4387 - Elapsed time: 1 ms </code></pre> <p>2.Run: with an enumerable range of 600000000:</p> <pre><code>Timer frequency in ticks per second = 3312851 Measuring 1 Total ticks: 29740243 - Elapsed time: 8977 ms Measuring 2 Total ticks: 33722438 - Elapsed time: 10179 ms Measuring 3 Total ticks: 77145502 - Elapsed time: 23286 ms Measuring 4 Total ticks: 120078284 - Elapsed time: 36246 ms Measuring 5 Total ticks: 30899585 - Elapsed time: 9327 ms </code></pre> <p>Interesting fact: using the Garbage Collector before performing the test script will increase the time for Measurement 4 vastly:</p> <p>3.Run: with an enumerable range of 600000000 and Garbage Collector (from LinqPad):</p> <pre><code>Timer frequency in ticks per second = 3312851 Measuring 1 Total ticks: 29597830 - Elapsed time: 8934 ms Measuring 2 Total ticks: 33532083 - Elapsed time: 10121 ms Measuring 3 Total ticks: 76403692 - Elapsed time: 23062 ms Measuring 4 Total ticks: 58534548 - Elapsed time: 17668 ms Measuring 5 Total ticks: 32943622 - Elapsed time: 9944 ms </code></pre> <p>In conclusion, can I say that method 1 is the most suitable option to perform small select queries and method 5 when the select delegates will increase? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload