Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to invalidate cache when benchmarking?
    primarykey
    data
    text
    <p>I have this code, that when swapping the order of UsingAs and UsingCast, their performance also swaps.</p> <pre><code>using System; using System.Diagnostics; using System.Linq; using System.IO; class Test { const int Size = 30000000; static void Main() { object[] values = new MemoryStream[Size]; UsingAs(values); UsingCast(values); Console.ReadLine(); } static void UsingCast(object[] values) { Stopwatch sw = Stopwatch.StartNew(); int sum = 0; foreach (object o in values) { if (o is MemoryStream) { var m = (MemoryStream)o; sum += (int)m.Length; } } sw.Stop(); Console.WriteLine("Cast: {0} : {1}", sum, (long)sw.ElapsedMilliseconds); } static void UsingAs(object[] values) { Stopwatch sw = Stopwatch.StartNew(); int sum = 0; foreach (object o in values) { if (o is MemoryStream) { var m = o as MemoryStream; sum += (int)m.Length; } } sw.Stop(); Console.WriteLine("As: {0} : {1}", sum, (long)sw.ElapsedMilliseconds); } } </code></pre> <p>Outputs:</p> <pre><code>As: 0 : 322 Cast: 0 : 281 </code></pre> <p>When doing this...</p> <pre><code>UsingCast(values); UsingAs(values); </code></pre> <p>...Results to this:</p> <pre><code>Cast: 0 : 322 As: 0 : 281 </code></pre> <p>When doing just this...</p> <pre><code>UsingAs(values); </code></pre> <p>...Results to this:</p> <pre><code>As: 0 : 322 </code></pre> <p>When doing just this:</p> <pre><code>UsingCast(values); </code></pre> <p>...Results to this:</p> <pre><code>Cast: 0 : 322 </code></pre> <p>Aside from running them independently, how to <strong>invalidate</strong> the cache so the second code being benchmarked won't receive the cached memory of first code?</p> <p>Benchmarking aside, just loved the fact that modern processors do this caching magic :-)</p> <p><strong>[EDIT]</strong></p> <p>As advised to try this faster code(supposedly)...</p> <pre><code>static void UsingAsAndNullTest(object[] values) { Stopwatch sw = Stopwatch.StartNew(); int sum = 0; foreach (object o in values) { var m = o as MemoryStream; if (m != null) { sum += (int)m.Length; } } sw.Stop(); Console.WriteLine("As and null test: {0} : {1}", sum, (long)sw.ElapsedMilliseconds); } </code></pre> <p>...The result is this:</p> <pre><code>As and null test: 0 : 342 </code></pre> <p>Slower than the two codes above</p> <p><strong>[EDIT]:</strong></p> <p>As advised to hand each routine their own copy...</p> <pre><code>static void UsingAs(object[] values) { object[] a = values.ToArray(); Stopwatch sw = Stopwatch.StartNew(); int sum = 0; foreach (object o in a) { if (o is MemoryStream) { var m = o as MemoryStream; sum += (int)m.Length; } } sw.Stop(); Console.WriteLine("As: {0} : {1}", sum, (long)sw.ElapsedMilliseconds); } static void UsingCast(object[] values) { object[] a = values.ToArray(); Stopwatch sw = Stopwatch.StartNew(); int sum = 0; foreach (object o in a) { if (o is MemoryStream) { var m = (MemoryStream)o; sum += (int)m.Length; } } sw.Stop(); Console.WriteLine("Cast: {0} : {1}", sum, (long)sw.ElapsedMilliseconds); } </code></pre> <p>...Outputs:</p> <pre><code>Cast: 0 : 282 As: 0 : 282 </code></pre> <p>Now they have the same results, thanks Remus!</p> <p>Running Cast and As independently, they also yield the same result(i.e. 282). Now, as for why they become <strong>faster</strong> (from 322 down to 282 milliseconds) when they are handed their own copy of array, I can't make anything out of it :-) That's entirely another story</p>
    singulars
    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