显式算术,编译器会处理吗?

Explicit arithmetic, Does the compiler take care of it?

有时我发现通过明确算术更容易理解代码(对于将来你自己或其他人)。例如。如果您从其他地方添加 3 个值,而不是单个幻数 +6,则写 1+2+3。

这是低效的还是编译器 optimize/reduce 合适?我在考虑 C,但总的来说,这值得担心吗?

是的。所有合格的 C 编译器都将执行 constant folding optimizations where possible, replacing constant mathematical expressions with their results. In most compilers, this type of optimization is applied even when optimizations are otherwise disabled (e.g, -O0). Here's an example.

此行为不限于 C;大多数其他编译语言也会执行这种类型的优化。解释型语言通常不会,因为那里的好处不那么显着,并且其中一些可能具有语义,这可能使常量折叠成为不安全的优化(例如,允许基本操作在内置类型上被覆盖)。