Object.GetType性能() [英] Performance of Object.GetType()

查看:118
本文介绍了Object.GetType性能()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有很多在我们的应用程序记录电话。我们的记录仪需要一个System.Type的参数,以便它可以显示哪些组件创建呼叫。有时,当我们被人打扰,我们做的是这样的:

We have lots of logging calls in our app. Our logger takes a System.Type parameter so it can show which component created the call. Sometimes, when we can be bothered, we do something like:

class Foo
{
  private static readonly Type myType = typeof(Foo);

  void SomeMethod()
  {
     Logger.Log(myType, "SomeMethod started...");
  }
 }



由于这需要得到Type对象只有一次。然而,我们没有任何这实际指标。任何人有任何想法多少,这可节省超过调用this.GetType()每次我们登录的时间?

As this requires getting the Type object only once. However we don't have any actual metrics on this. Anyone got any idea how much this saves over calling this.GetType() each time we log?

(我知道我可以做我自己的指标,没有大问题,但哎,什么是计算器?)

(I realise I could do the metrics myself with no big problem, but hey, what's StackOverflow for?)

推荐答案

我强烈怀疑的GetType()将采取显著的时间比任何实际的记录。当然,还有,你对Logger.Log调用不会做任何实际的IO的可能性......我仍然怀疑的差别将是无关紧要虽然

I strongly suspect that GetType() will take significantly less time than any actual logging. Of course, there's the possibility that your call to Logger.Log won't do any actual IO... I still suspect the difference will be irrelevant though.

编辑:基准的代码是在底部。结果:

Benchmark code is at the bottom. Results:

typeof(Test): 2756ms
TestType (field): 1175ms
test.GetType(): 3734ms

这是调用方法100的亿的时间 - 优化收益几秒钟左右。我怀疑真正的测井方法将有很多工作要做,并调用了100万次将需要更长的时间超过4秒的总,即使不写什么了。 (我可能是错的,当然 - 你必须尝试自己。)

That's calling the method 100 million times - the optimisation gains a couple of seconds or so. I suspect the real logging method will have a lot more work to do, and calling that 100 million times will take a lot longer than 4 seconds in total, even if it doesn't write anything out. (I could be wrong, of course - you'd have to try that yourself.)

在换句话说,正常的,我会用最可读的代码去。而不是微观优化

In other words, as normal, I'd go with the most readable code rather than micro-optimising.

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

class Test
{
    const int Iterations = 100000000;

    private static readonly Type TestType = typeof(Test);

    static void Main()
    {
        int total = 0;
        // Make sure it's JIT-compiled
        Log(typeof(Test)); 

        Stopwatch sw = Stopwatch.StartNew();
        for (int i = 0; i < Iterations; i++)
        {
            total += Log(typeof(Test));
        }
        sw.Stop();
        Console.WriteLine("typeof(Test): {0}ms", sw.ElapsedMilliseconds);

        sw = Stopwatch.StartNew();
        for (int i = 0; i < Iterations; i++)
        {
            total += Log(TestType);
        }
        sw.Stop();
        Console.WriteLine("TestType (field): {0}ms", sw.ElapsedMilliseconds);

        Test test = new Test();
        sw = Stopwatch.StartNew();
        for (int i = 0; i < Iterations; i++)
        {
            total += Log(test.GetType());
        }
        sw.Stop();
        Console.WriteLine("test.GetType(): {0}ms", sw.ElapsedMilliseconds);
    }

    // I suspect your real Log method won't be inlined,
    // so let's mimic that here
    [MethodImpl(MethodImplOptions.NoInlining)]
    static int Log(Type type)
    {
        return 1;
    }
}

这篇关于Object.GetType性能()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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