[英]Is a += 5 faster than a = a + 5?
I'm currently learning about operators and expressions in C# and I understood that if I want to increment the value of a variable by 5, I can do it in two different ways: a = a + 5
and a += 5
.我目前正在学习 C# 中的运算符和表达式,我明白如果我想将变量的值增加 5,我可以通过两种不同的方式来实现:
a = a + 5
和a += 5
。 Apparently, the second way is easier and faster to write and more pleasant to read.显然,第二种方式写起来更容易、更快,读起来更愉快。 However, computer-wise, is
a += 5
faster than a = a + 5
?但是,在计算机方面,
a += 5
比a = a + 5
快? Does it take less time to compile and execute than the longer version of the expression?与较长版本的表达式相比,编译和执行所需的时间是否更少?
However, computer-wise, is a += 5 faster than a = a + 5?
但是,在计算机方面,a += 5 是否比 a = a + 5 快?
Both are same, first( a += 5
) is equal to second a = a + 5
.两者都是相同的,first(
a += 5
) 等于第二个a = a + 5
。
You may see:你可能会看到:
+= Operator (C# Reference) += 运算符(C# 参考)
An expression using the
+=
assignment operator, such asx += y
is equivalent tox = x + y
except that x is only evaluated once .使用
+=
赋值运算符的表达式,例如x += y
等价于x = x + y
只是 x 只计算一次。 The meaning of the + operator depends on the types of x and y (addition for numeric operands, concatenation for string operands, and so forth).+ 运算符的含义取决于 x 和 y 的类型(数字操作数的加法、字符串操作数的串联等)。
So it depends on type of a
and in those situations where multiple threads are accessing your variable a
you could get different results.因此,这取决于
a
类型,并且在多个线程访问您的变量a
情况下,您可能会得到不同的结果。 But for most of the other case it would be same:但对于大多数其他情况,它会是一样的:
For code:对于代码:
static void Main(string[] args)
{
int a = 10;
a += 5;
Console.WriteLine(a);
}
Build in Release Mode the IL is在发布模式下构建 IL
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 14 (0xe)
.maxstack 2
.locals init ([0] int32 a)
IL_0000: ldc.i4.s 10
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: ldc.i4.5
IL_0005: add
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: call void [mscorlib]System.Console::WriteLine(int32)
IL_000d: ret
} // end of method Program::Main
Same IL is generated through code:通过代码生成相同的 IL:
static void Main(string[] args)
{
int a = 10;
a = a + 5;
Console.WriteLine(a);
}
IL (same) is: IL(相同)是:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 14 (0xe)
.maxstack 2
.locals init ([0] int32 a)
IL_0000: ldc.i4.s 10
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: ldc.i4.5
IL_0005: add
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: call void [mscorlib]System.Console::WriteLine(int32)
IL_000d: ret
} // end of method Program::Main
That depends on what a
is.这取决于
a
是什么。 a = a + 5
evaluates a
twice. a = a + 5
对a
求值两次。 a += 5
evaluates a
exactly once . a += 5
求值a
正好一次。
If a
is an integer, that difference is likely not to matter in most cases, although not strictly all cases.如果
a
是一个整数,那么在大多数情况下这种差异可能无关紧要,尽管并非严格意义上的所有情况。 If, for example, a
is accessed from multiple threads then the exact types and windows for race conditions can differ.例如,如果从多个线程访问
a
则竞争条件的确切类型和窗口可能不同。
On top of that, if evaluating the expression causes side effects it's the difference between those side effects being observed once versus being observed twice.最重要的是,如果评估表达式会导致副作用,则是观察一次与观察两次的副作用之间的差异。 That can, under certain circumstances, be a big deal, possibly affecting the correctness of the code, not just its speed.
在某些情况下,这可能是一个大问题,可能会影响代码的正确性,而不仅仅是速度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.