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

前端之家收集整理的这篇文章主要介绍了c# – 我可以比较IL代码来确定哪种技术更快或更好?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
背景

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

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

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

制作:

  1. IL_0001: ldc.i4.4
  2. IL_0002: newarr System.String
  3. IL_0007: stloc.2
  4. IL_0008: ldloc.2
  5. IL_0009: ldc.i4.0
  6. IL_000A: ldstr "1"
  7. IL_000F: stelem.ref
  8. IL_0010: ldloc.2
  9. IL_0011: ldc.i4.1
  10. IL_0012: ldstr "2"
  11. IL_0017: stelem.ref
  12. IL_0018: ldloc.2
  13. IL_0019: ldc.i4.2
  14. IL_001A: ldstr "3"
  15. IL_001F: stelem.ref
  16. IL_0020: ldloc.2
  17. IL_0021: ldc.i4.3
  18. IL_0022: ldstr "4"
  19. IL_0027: stelem.ref
  20. IL_0028: ldloc.2
  21. IL_0029: stloc.0
  22. IL_002A: ldloc.0
  23. IL_002B: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
  24. IL_0030: brtrue.s IL_0045
  25. IL_0032: ldnull
  26. IL_0033: ldftn b__0
  27. IL_0039: newobj System.Converter<System.String,System.Int32>..ctor
  28. IL_003E: stsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
  29. IL_0043: br.s IL_0045
  30. IL_0045: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
  31. IL_004A: call System.Array.ConvertAll
  32. IL_004F: stloc.1
  33.  
  34. b__0:
  35. IL_0000: ldarg.0
  36. IL_0001: call System.Int32.Parse
  37. IL_0006: stloc.0
  38. IL_0007: br.s IL_0009
  39. IL_0009: ldloc.0
  40. IL_000A: ret

和另一个答案:

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

制作:

  1. IL_0001: ldc.i4.4
  2. IL_0002: newarr System.String
  3. IL_0007: stloc.2
  4. IL_0008: ldloc.2
  5. IL_0009: ldc.i4.0
  6. IL_000A: ldstr "1"
  7. IL_000F: stelem.ref
  8. IL_0010: ldloc.2
  9. IL_0011: ldc.i4.1
  10. IL_0012: ldstr "2"
  11. IL_0017: stelem.ref
  12. IL_0018: ldloc.2
  13. IL_0019: ldc.i4.2
  14. IL_001A: ldstr "3"
  15. IL_001F: stelem.ref
  16. IL_0020: ldloc.2
  17. IL_0021: ldc.i4.3
  18. IL_0022: ldstr "4"
  19. IL_0027: stelem.ref
  20. IL_0028: ldloc.2
  21. IL_0029: stloc.0
  22. IL_002A: ldloc.0
  23. IL_002B: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
  24. IL_0030: brtrue.s IL_0045
  25. IL_0032: ldnull
  26. IL_0033: ldftn b__0
  27. IL_0039: newobj System.Func<System.String,System.Int32>..ctor
  28. IL_003E: stsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
  29. IL_0043: br.s IL_0045
  30. IL_0045: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
  31. IL_004A: call System.Linq.Enumerable.Select
  32. IL_004F: call System.Linq.Enumerable.ToArray
  33. IL_0054: stloc.1
  34.  
  35. b__0:
  36. IL_0000: ldarg.0
  37. IL_0001: call System.Int32.Parse
  38. IL_0006: stloc.0
  39. IL_0007: br.s IL_0009
  40. IL_0009: ldloc.0
  41. 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个操作非常非常快,而最后几个操作几乎占用了所有可执行时间.

猜你在找的C#相关文章