加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 我可以比较IL代码来确定哪种技术更快或更好?

发布时间:2020-12-15 08:30:16 所属栏目:百科 来源:网络整理
导读:背景 This question 让我想到了什么.最近,由于我一直在研究linq pad的IL功能,我一直在将两种方法的IL代码与同一问题进行比较,以“确定”最佳. 使用上面链接的问题,关于转换数组,我为两个答案生成了IL代码: var arr = new string[] { "1","2","3","4" };var
背景

This question让我想到了什么.最近,由于我一直在研究linq pad的IL功能,我一直在将两种方法的IL代码与同一问题进行比较,以“确定”最佳.

使用上面链接的问题,关于转换数组,我为两个答案生成了IL代码:

var arr = new string[] { "1","2","3","4" };
var result = Array.ConvertAll(arr,s => Int32.Parse(s));

制作:

IL_0001:  ldc.i4.4    
IL_0002:  newarr      System.String
IL_0007:  stloc.2     
IL_0008:  ldloc.2     
IL_0009:  ldc.i4.0    
IL_000A:  ldstr       "1"
IL_000F:  stelem.ref  
IL_0010:  ldloc.2     
IL_0011:  ldc.i4.1    
IL_0012:  ldstr       "2"
IL_0017:  stelem.ref  
IL_0018:  ldloc.2     
IL_0019:  ldc.i4.2    
IL_001A:  ldstr       "3"
IL_001F:  stelem.ref  
IL_0020:  ldloc.2     
IL_0021:  ldc.i4.3    
IL_0022:  ldstr       "4"
IL_0027:  stelem.ref  
IL_0028:  ldloc.2     
IL_0029:  stloc.0     
IL_002A:  ldloc.0     
IL_002B:  ldsfld      UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_0030:  brtrue.s    IL_0045
IL_0032:  ldnull      
IL_0033:  ldftn       b__0
IL_0039:  newobj      System.Converter<System.String,System.Int32>..ctor
IL_003E:  stsfld      UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_0043:  br.s        IL_0045
IL_0045:  ldsfld      UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_004A:  call        System.Array.ConvertAll
IL_004F:  stloc.1     

b__0:
IL_0000:  ldarg.0     
IL_0001:  call        System.Int32.Parse
IL_0006:  stloc.0     
IL_0007:  br.s        IL_0009
IL_0009:  ldloc.0     
IL_000A:  ret

和另一个答案:

var arr = new string[] { "1","4" };
var result = arr.Select(s => int.Parse(s)).ToArray();

制作:

IL_0001:  ldc.i4.4    
IL_0002:  newarr      System.String
IL_0007:  stloc.2     
IL_0008:  ldloc.2     
IL_0009:  ldc.i4.0    
IL_000A:  ldstr       "1"
IL_000F:  stelem.ref  
IL_0010:  ldloc.2     
IL_0011:  ldc.i4.1    
IL_0012:  ldstr       "2"
IL_0017:  stelem.ref  
IL_0018:  ldloc.2     
IL_0019:  ldc.i4.2    
IL_001A:  ldstr       "3"
IL_001F:  stelem.ref  
IL_0020:  ldloc.2     
IL_0021:  ldc.i4.3    
IL_0022:  ldstr       "4"
IL_0027:  stelem.ref  
IL_0028:  ldloc.2     
IL_0029:  stloc.0     
IL_002A:  ldloc.0     
IL_002B:  ldsfld      UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_0030:  brtrue.s    IL_0045
IL_0032:  ldnull      
IL_0033:  ldftn       b__0
IL_0039:  newobj      System.Func<System.String,System.Int32>..ctor
IL_003E:  stsfld      UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_0043:  br.s        IL_0045
IL_0045:  ldsfld      UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_004A:  call        System.Linq.Enumerable.Select
IL_004F:  call        System.Linq.Enumerable.ToArray
IL_0054:  stloc.1     

b__0:
IL_0000:  ldarg.0     
IL_0001:  call        System.Int32.Parse
IL_0006:  stloc.0     
IL_0007:  br.s        IL_0009
IL_0009:  ldloc.0     
IL_000A:  ret

看看这个,我能说的是后者的选择

>需要额外增加1行
>当第一个答案没有时,使用linq
>通过IL_0039以不同方式创建Int.

问题

>对于这个具体的例子,我的假设是否正确?
>一般来说,我应该如何通过IL代码比较两个解决方案?
>一般来说,IL LOC较少的解决方案是否意味着它会更快或使用更少的内存?
>正如标题所说,我可以比较IL代码来确定哪种技术更快或更好?

FWIW,我不经常这样做,只是在极少数情况下,当开发人员在工作中进行一些讨论时.有人会说“哦这更有效”,我们会把它扔进linqpad来查看IL代码. FWIW,在获得高效/快速的方法之前,我几乎总是遵守它的工作原理.只是这样人们不认为我不断比较我正在开发的IL代码:)

解决方法

  • For this specific example,are my assumptions correct?
  • In general,how should I go about comparing two solutions via IL code?
  • In general,does a solution with fewer IL LOC mean that it will be faster or use less memory?
  • As the title says,Can I compare IL code to determine which technique is faster or better?

1)您的假设对于正在发生的事情是正确的.

2)您需要了解IL代码正在做什么才能确定哪个“更好”

3)否.这意味着它需要更少的运行指令.但是,这些单独的指令可能会使用更多内存或更少.例如,您引用的指令(在一种情况下是创建Func委托),另一种情况是创建Converter对象.没有更多信息,很难说这两件事中哪一件更贵.

4)是和否….

问题是IL代码会告诉你发生了什么,但实际上IL中的嵌套调用将成为大型性能驱动程序.如果IL代码在任何地方都进行简单操作,通常越短越好(尽管单个IL操作的速度本身可能不同).当代码调用其他类型的方法或构造函数时,例如你的代码或构造函数,就不可能单独告诉它.在一种情况下,IL的一行可能需要更长的时间(例如,如果它调用的是昂贵的方法),而在另一种情况下(其中它们正在进行简单的操作)需要花费50.

例如,在上面的例子中,前20个操作非常非常快,而最后几个操作几乎占用了所有可执行时间.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读