不必要的花括号会降低性能吗?

spa*_*a93 5 c# syntax optimization performance

在编程之后最近遇到这个,我一直在想这个.以下是两个合法和编译的片段.具体来说,我的问题是这个..在第二种情况下,括号是否使程序变慢?为什么允许这样做呢?

第一例:

if (statement)
{
 // do something
}
Run Code Online (Sandbox Code Playgroud)

第二个案例:

{
    if (statement)
    {
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)

另外如果我有类似下面的代码的话.运行时是否与调用函数X相同而没有任何大括号.

{
  {
    {
      // call function X
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 12

大多数时候它没有任何区别 - 你绝对应该编写可读性的代码.

然而,花括号可以以惊人的方式对性能产生影响,尽管这很不寻常.考虑以下代码:

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);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

额外的括号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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的区别是:

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

这有点特定于实现,但它表明冗余外观可能会产生影响.