使用过度的代表为性能的一个坏主意? [英] Is using delegates excessively a bad idea for performance?

查看:118
本文介绍了使用过度的代表为性能的一个坏主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下code:

if (IsDebuggingEnabled) { 
   instance.Log(GetDetailedDebugInfo()); 
}

GetDetailedDebugInfo()可能是一个昂贵的方法,所以我们只需要调用它,如果我们在调试模式下正在运行。

GetDetailedDebugInfo() may be an expensive method, so we only want to call it if we're running in debug mode.

现在,清洁的替代方法是code是这样的:

Now, the cleaner alternative is to code something like this:

instance.Log(() => GetDetailedDebugInfo());

在哪里.LOG()的定义,如:

Where .Log() is defined such as:

public void Log(Func<string> getMessage)
{
    if (IsDebuggingEnabled) 
    {
        LogInternal(getMessage.Invoke());
    }
}

我关心的是性能,preliminary测试不显示第二种情况是特别贵,但我不希望碰到任何的意外,如果负荷增加。

My concern is with performance, preliminary testing doesn't show the second case to be particularly more expensive, but I don't want to run into any surprises if load increases.

哦,请不要暗示条件编译,因为它并不适用于这种情况。

Oh, and please don't suggest conditional compilation because it doesn't apply to this case.

(PS:我直接在StackOverflow上写的code提问textarea的,所以不要怪我,如果有细微的错误,它并不能编译,你明白了吧:)

(P.S.: I wrote the code directly in the StackOverflow Ask a Question textarea so don't blame me if there are subtle bugs and it doesn't compile, you get the point :)

推荐答案

我希望了解有关在这种情况下的表现有些文档,但似乎我的一切都是关于如何提高我的code建议...似乎没有人读过我的PS - 无分你

I was hoping for some documentation regarding performance in such cases, but it seems that all I got were suggestions on how to improve my code... No one seems to have read my P.S. - no points for you.

所以我写了一个简单的测试案例:

So I wrote a simple test case:

    public static bool IsDebuggingEnabled { get; set; }


    static void Main(string[] args)
    {
        for (int j = 0; j <= 10; j++)
        {
            Stopwatch sw = Stopwatch.StartNew();
            for (int i = 0; i <= 15000; i++)
            {
                Log(GetDebugMessage);
                if (i % 1000 == 0) IsDebuggingEnabled = !IsDebuggingEnabled;
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
        }

        Console.ReadLine();
        for (int j = 0; j <= 10; j++)
        {
            Stopwatch sw = Stopwatch.StartNew();
            for (int i = 0; i <= 15000; i++)
            {
                if (IsDebuggingEnabled) GetDebugMessage();
                if (i % 1000 == 0) IsDebuggingEnabled = !IsDebuggingEnabled;
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
        }
        Console.ReadLine();
    }

    public static string GetDebugMessage()
    {
        StringBuilder sb = new StringBuilder(100);
        Random rnd = new Random();
        for (int i = 0; i < 100; i++)
        {
            sb.Append(rnd.Next(100, 150));
        }
        return sb.ToString();
    }

    public static void Log(Func<string> getMessage)
    {
        if (IsDebuggingEnabled)
        {
            getMessage();
        }
    }

计时似乎是完全相同的两个版本之间的相同。 我得到145毫秒,在第一种情况下,145毫秒,在第二种情况下

Timings seem to be exactly the same between the two versions. I get 145 ms in the first case, and 145 ms in the second case

看起来像我回答我自己的问题。

Looks like I answered my own question.

这篇关于使用过度的代表为性能的一个坏主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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