感叹一下:为什么CLR可以保持效率

 

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  IL_Learn
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
int a = 1;
            
int b = 2;

            
float c;

            c 
= a + b;

            
float d = 0;
            d 
= a + b + c;
        }

    }

}

编译后ildasm /adv一下,得到:

.method private hidebysig static void  Main(string []  args) cil managed
{
  .entrypoint
  // Code size       
24  (0x18)
  .maxstack  
2
  .locals init (int32 V_0
,
           int32 V_1
,
           float32 V_2
,
           float32 V_3)
  IL_0000:  nop
  IL_0001:  ldc.i4
.1
  IL_0002:  stloc
.0
  IL_0003:  ldc.i4
.2
  IL_0004:  stloc
.1
  IL_0005:  ldloc
.0
  IL_0006:  ldloc
.1
  IL_0007:  add
  IL_0008:  conv.r4
  IL_0009:  stloc
.2
  IL_000a:  ldc.r4     
0.0
  IL_000f:  stloc
.3
  IL_0010:  ldloc
.0
  IL_0011:  ldloc
.1
  IL_0012:  add
  IL_0013:  conv.r4
  IL_0014:  ldloc
.2
  IL_0015:  add
  IL_0016:  stloc
.3
  IL_0017:  ret
} // end of method Program::Main

原来,每次要进行类型转换,都有csc产生的conv.r4,意思是将栈顶的一个家伙转型为float型,哎,CLR懒死了,连这条指令都由CSC产生!不过,这样,把尽量多的工作交给编译器完成,CLR只是简单的JIT和执行,从而保证了效率。还是应该佩服一下的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值