不必要的花括号会降低性能吗? [英] Do unnecessary curly braces reduce performance?
问题描述
最近在编程时遇到了这个问题,我一直对此感到疑惑.以下是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
中使用的每个捕获类实例都包含两个变量,即使每个委托只实际使用其中一个变量,而在MoreCurlies
中,每个捕获类仅捕获单个变量
这在某种程度上都是特定于实现的,但是它表明看起来多余的小卷发可以产生影响.
After recently encountering this while programming, I have been wondering about this. Below are 2 snippets which are both legal and compile. Specifically, my question is this.. in the second case, do the brackets make the program slower? Also why is this allowed?
1st case:
if (statement)
{
// do something
}
2nd case:
{
if (statement)
{
// do something
}
}
Additionally what if I had something like the code below.. Is the runtime the same as just calling function X without any of the curly brackets.
{
{
{
// call function X
}
}
}
Most of the time it doesn't make any difference - and you should definitely code for readability more than anything else.
However, curly braces can have an effect on performance, in a surprising way, although it's pretty unusual. Consider this code:
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);
}
}
}
}
}
The extra braces in MoreCurlies
look redundant, right? Not quite... the generated code looks more like this:
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;
}
}
}
The differences here are:
- An instance of the capture class is created in each iteration of the loop in
FewerCurlies
, even if it's not used - Each instance of the capture class used in
FewerCurlies
contains both variables, even though each delegate will only actually use one of them, whereas inMoreCurlies
each capture class only captures a single variable
This is all somewhat implementation-specific, but it shows that redundant-looking curlies can have an impact.
这篇关于不必要的花括号会降低性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!