c# - 不必要的大括号会降低性能吗?

标签 c# performance optimization syntax

最近在编程的时候遇到这个问题,一直在想这个问题。以下是 2 个合法且可编译的片段。具体来说,我的问题是......在第二种情况下,括号是否会使程序变慢?还有为什么这是允许的?

第一种情况:

if (statement)
{
 // do something
}

第二种情况:

{
    if (statement)
    {
        // do something
    }
}

此外,如果我有类似下面的代码会怎样……运行时是否与不带任何大括号调用函数 X 相同。

{
  {
    {
      // call function X
    }
  }
}

最佳答案

大多数时间它没有任何区别 - 你绝对应该为可读性编写代码而不是其他任何东西。

但是,花括号可以以令人惊讶的方式影响性能,尽管这很不寻常。考虑这段代码:

using System;
using System.Collections.Generic;

class Test
{
    static void FewerCurlies()
    {
        List<Action> actions = new List<Action>();
        for (int i = 0; i < 100; i++)
        {
            int x;
            if (i % 3 == 0)
            {
                actions.Add(() => x = 10);
            }

            int y;
            if (i % 3 == 1)
            {
                actions.Add(() => y = 10);
            }
        }
    }

    static void MoreCurlies()
    {
        List<Action> actions = new List<Action>();
        for (int i = 0; i < 100; i++)
        {
            {
                int x;
                if (i % 3 == 0)
                {
                    actions.Add(() => x = 10);
                }
            }

            {
                int y;
                if (i % 3 == 1)
                {
                    actions.Add(() => y = 10);
                }
            }
        }
    }
}

MoreCurlies 中的额外大括号看起来多余,对吧?不完全是……生成的代码看起来更像这样:

using System;
using System.Collections.Generic;

class Test
{
    static void FewerCurlies()
    {
        List<Action> actions = new List<Action>();
        for (int i = 0; i < 100; i++)
        {
            FewerCurliesCapture capture = new FewerCurliesCapture();
            if (i % 3 == 0)
            {
                actions.Add(capture.Method1);
            }

            if (i % 3 == 1)
            {
                actions.Add(capture.Method2);
            }
        }
    }

    static void MoreCurlies()
    {
        List<Action> actions = new List<Action>();
        for (int i = 0; i < 100; i++)
        {
            {
                MoreCurliesCapture1 capture = new MoreCurliesCapture1();
                if (i % 3 == 0)
                {
                    actions.Add(capture.Method);
                }
            }

            {
                MoreCurliesCapture1 capture = new MoreCurliesCapture2();
                if (i % 3 == 1)
                {
                    actions.Add(capture.Method);
                }
            }
        }
    }

    private class FewerCurliesCapture
    {
        public int x;
        public int y;

        public void Method1()
        {
            x = 10;
        }

        public void Method2()
        {
            y = 10;
        }
    }

    private class MoreCurliesCapture1
    {
        public int x;

        public void Method()
        {
            x = 10;
        }
    }

    private class MoreCurliesCapture2
    {
        public int y;

        public void Method()
        {
            y = 10;
        }
    }
}

这里的区别是:

  • 捕获类的实例在 FewerCurlies 循环的每次迭代中创建,即使它没有被使用
  • FewerCurlies 中使用的捕获类的每个实例都包含两个变量,即使每个委托(delegate)实际上只会使用其中一个,而在 MoreCurlies 中每个捕获类只捕获单个变量

这在某种程度上是特定于实现的,但它表明看起来多余的 curl 可以产生影响。

关于c# - 不必要的大括号会降低性能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31412828/

相关文章:

c++ - 如何有效地扫描 2 位掩码交替每次迭代

mysql - 避免子选择的查询

optimization - 使用 CUDA 进行蒙特卡洛优化

c# - 显示由外键引用到 userprofile 表的表中的项目列表

c# - 异常会传回调用方法吗?

c# - 如何跟踪业务对象的变化?

c# - 如何在 ViewModel 中获取控件的高度

ios - 升级到 Mavericks 后 Xcode 编译时间非常慢

Android Listview 与 sqlite 和 CursorAdapter

mysql - 这个MYSQL可以优化吗?