代码之家  ›  专栏  ›  技术社区  ›  Allen Rice 0x6A75616E

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

  •  6
  • Allen Rice 0x6A75616E  · 技术社区  · 16 年前

    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", "2", "3", "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     
    

    • 通过IL_0039以不同的方式创建Int。

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

    2 回复  |  直到 9 年前
        1
  •  8
  •   Reed Copsey    16 年前
    • 一般来说,我应该如何通过IL代码比较两个解决方案?
    • 一般来说,IL LOC较少的解决方案是否意味着它会更快或使用更少的内存?

    1) 你对正在发生的事情的假设是正确的。

    2) 您需要了解IL代码的作用,以确定哪个“更好”

    3) 不,这意味着运行指令更少。但是,这些单独的指令可能会使用更多或更少的内存。例如,您引用的指令,在一种情况下是创建一个Funcdelegate,在另一种情况中是创建Converter对象。如果没有更多的信息,很难判断这两件事中哪一件更贵。

        2
  •  3
  •   Franci Penov    16 年前

    两个答案的大部分工作都在IL 004A完成(第二个答案在IL 004F完成)。除非你知道这些外部调用的成本,否则你就没有实际的基础来比较这两个答案的性能。