.NET乘法优化 [英] .NET multiplication optimization

查看:129
本文介绍了.NET乘法优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译器是否优化任何乘法1?也就是说,考虑:

Does the compiler optimize out any multiplications by 1? That is, consider:

int a = 1;
int b = 5 * a;

将表达式5 * a优化到5?如果不是,如果a被定义为:

Will the expression 5 * a be optimized into just 5? If not, will it if a is defined as:

const int a = 1;


推荐答案

它会在编译时预先计算任何常量表达式,包括字符串连接。如果没有 const ,它将被单独使用。

It will pre-calculate any constant expressions when it compiles, including string concatenation. Without the const it will be left alone.

IL:

.maxstack 2
.locals init ([0] int32, [1] int32)

ldc.i4.1   //load 1
stloc.0    //store in 1st local variable
ldc.i4.5   //load 5
ldloc.0    //load 1st variable
mul        // 1 * 5
stloc.1    // store in 2nd local variable 


b $ b

第二个示例编译为:

The second example compiles to this:

.maxstack 1
.locals init ( [0] int32 )

ldc.i4.5 //load 5 
stloc.0  //store in local variable

这篇关于.NET乘法优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆