如何当标杆缓存失效? [英] How to invalidate cache when benchmarking?

查看:185
本文介绍了如何当标杆缓存失效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code,即交换UsingAs和UsingCast的订单时,他们的表现也交换。

I have this code, that when swapping the order of UsingAs and UsingCast, their performance also swaps.

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


}

输出:

As: 0 : 322
Cast: 0 : 281

在做这个...

UsingCast(values);
UsingAs(values);

......结果这样:

...Results to this:

Cast: 0 : 322
As: 0 : 281

这样当眼前这个...

When doing just this...

UsingAs(values);

......结果这样:

...Results to this:

As: 0 : 322

当这样做只是这:

UsingCast(values);

......结果这样:

...Results to this:

Cast: 0 : 322

除了独立运行的他们,如何的无效缓存,以便被基准将不会收到第一个code的缓存内存第二code?

Aside from running them independently, how to invalidate the cache so the second code being benchmarked won't receive the cached memory of first code?

标杆不谈,只是喜欢一个事实,即现代处理器做缓存魔法: - )

Benchmarking aside, just loved the fact that modern processors do this caching magic :-)

至于建议试试这个快code(据说)...

As advised to try this faster code(supposedly)...

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

...结果是这样的:

...The result is this:

As and null test: 0 : 342

比两个codeS上面放缓

Slower than the two codes above

诚如手每个例程自己的副本...

As advised to hand each routine their own copy...

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

...输出:

Cast: 0 : 282
As: 0 : 282

现在他们有相同的效果,由于莱姆斯!

Now they have the same results, thanks Remus!

运行演作为独立的,他们也产生同样的结果(即282)。现在,为什么他们成为的更快(从322到282毫秒)时,他们都交给各自为阵的副本,我什么都做不了了吧:-)那完全是另一回事

Running Cast and As independently, they also yield the same result(i.e. 282). Now, as for why they become faster (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

推荐答案

如果你要拿出照片的L2缓存和TLB缺失则只需拨打一个不同的MemoryStream相同尺寸的第二次测试。

If you want to take out of the picture the L2 cache and the TLB misses then simply call the second test on a different MemoryStream of the same size.

这篇关于如何当标杆缓存失效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆