在子块内声明变量会提高性能吗?

发布于 2024-09-13 06:34:06 字数 592 浏览 13 评论 0原文

在 C# 中,比较以下三个替代方案时,性能会有什么差异吗?

一个

void ONE(int x) {

if (x == 10) 
{
    int y = 20;
    int z = 30;
    // do other stuff
} else {
    // do other stuff
}
}

两个

void TWO(int x) {

int y;
int z;

if (x == 10) 
{
    y = 20;
    z = 30;
    // do other stuff
} else {
    // do other stuff
}
}

三个

void THREE(int x) {

int y = 20;
int z = 30;

if (x == 10) 
{
    // do other stuff
} else {
    // do other stuff
}
}

In C#, would there be any difference in performance when comparing the following THREE alternatives?

ONE

void ONE(int x) {

if (x == 10) 
{
    int y = 20;
    int z = 30;
    // do other stuff
} else {
    // do other stuff
}
}

TWO

void TWO(int x) {

int y;
int z;

if (x == 10) 
{
    y = 20;
    z = 30;
    // do other stuff
} else {
    // do other stuff
}
}

THREE

void THREE(int x) {

int y = 20;
int z = 30;

if (x == 10) 
{
    // do other stuff
} else {
    // do other stuff
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倒数 2024-09-20 06:34:06

其他所有条件都相同(而且它们通常不是,这就是为什么您通常必须实际测试它),ONE()TWO() 应该生成相同的 IL 指令,因为局部变量最终作用于整个方法。如果 x==10THREE() 的速度会可忽略不计变慢,因为另外两个函数不会将值存储在局部变量中。

这三个变量占用相同的内存量——即使变量中没有存储任何内容,也会为所有变量分配内存。不过,如果 JIT 编译器查找未使用的变量,它可能会在此处执行优化。

All else being equal (and they usually aren't, which is why you normally have to actually test it), ONE() and TWO() should generate the same IL instructions since local variables end up scoped to the whole method. THREE() will be negligibly slower if x==10 since the other two won't bother to store the values in the local variables.

All three take up the same amount of memory—the memory for all variables is allocated even if nothing is stored in them. The JIT compiler may perform an optimization here, though, if it ever looks for unused variables.

初懵 2024-09-20 06:34:06

没有性能差异,但您会发现每个示例之间存在变量范围问题。

您还在这些示例之间展示了三种不同的意图,这不是您想要的:

  1. y 和 z 仅限于 if 语句的范围。

  2. y 和 z 在 if 语句外部使用,但有条件设置。

  3. y 和 z 与 if 语句无关。

There no performance difference, but you're going to find variable scope issues between each of those examples.

You're also showing three different intents between those examples, which isn't what you want:

  1. y and z are limited to the scope of the if statement.

  2. y and z are used outside of the if statement, but are set conditionally.

  3. y and z have nothing to do with the if statement whatsoever.

猫瑾少女 2024-09-20 06:34:06

当然,您应该始终选择一个,它更具可读性。它的速度快了几分之一纳秒并不是偶然的,可读的代码通常是偶然的。

Of course, you should always pick ONE, it is much more readable. That it is faster by a fraction of a nanosecond isn't an accident, readable code often is.

东北女汉子 2024-09-20 06:34:06

我认为这不会有太大区别。您唯一需要担心的是创建新对象并初始化它的成本是否昂贵。您总是可以尝试对每种方法进行数千次分析,以查看是否存在任何差异,但我怀疑您会发现任何差异。

我唯一一次将声明移到远离它的使用位置的地方是如果它要在循环中处理。例如:

void RunMethod() {
  FormRepresentation formRep = null;
  for (int idx = 0; idx < 10; idx++) {
    formRep = new FormRepresentation();
    // do something
  }
}

它实际上没有任何区别,因为对象仍在创建中,但对我来说,它看起来更干净。您需要考虑的另一件事是变量的范围。声明的变量不能在其声明范围之外使用。

I don't think it'll make much difference. The only time you would need to worry is if creating the new object and initializing it is expensive. You could always try to profile each method a couple thousand times to see if there are any differences but I doubt you'll find any.

The only time I move a declaration further away from where it's used is if it'll be worked on in a loop. e.g.:

void RunMethod() {
  FormRepresentation formRep = null;
  for (int idx = 0; idx < 10; idx++) {
    formRep = new FormRepresentation();
    // do something
  }
}

It doesn't actually make any difference since the object is still being created but, to me, it looks cleaner. The other thing you need to consider is the scope of the variable. Declared variables cannot be used outside the scope they were declared in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文