- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
背景
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
看到这里,我只能说是后一种选择
问题
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 委托(delegate),而在另一种情况下是创建 Converter 对象。在没有更多信息的情况下,很难判断这两种东西中哪一种更贵。
4) 是和不是....
问题是 IL 代码会告诉您发生了什么,但实际上 IL 中的嵌套调用将成为大型性能驱动程序。如果 IL 代码到处都在做简单的操作,一般来说越短越好(尽管个别 IL 操作本身的速度可能会有所不同)。当代码调用其他类型(例如您的类型)的方法或构造函数时,仅凭这一点就无法分辨。在一种情况下(例如,如果它正在调用一个昂贵的方法),一行 IL 可能比在另一种情况下(他们正在执行简单操作)花费 50 行更长的时间。
例如,在您上面的案例中,前 20 个操作非常非常快,而后几个操作几乎占用了您所有的可执行时间。
关于c# - 我可以比较 IL 代码以确定哪种技术更快或更好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1297325/
据我所知,根本不为元素呈现 HTML,或添加 display:none,似乎具有完全相同的行为:两者都使元素消失并且不与 HTML 交互。 我正在尝试禁用和隐藏一个复选框。所以HTML的总量很小;我无
我刚刚读了Android Architecture Tutorial: Developing an App with a Background Service (using IPC) .基本上是 让服
我有两个查询具有相同的结果,现在我想知道哪个查询更优化? 在选择中: select t1.*, sum(t2.value) as total_votes from table1 t1 left joi
有人告诉我,对于 I/O 绑定(bind)的应用程序,非阻塞 I/O 会更好。对于 CPU 密集型应用程序,阻塞 I/O 会好得多。我找不到这种说法的原因。试过谷歌,但很少有文章只是触及这个话题而没有
我有一个算法可以在数字列表中寻找好的对。一个好的配对被认为是索引 i 小于 j 且 arr[i] 1: # Finding the mid of the array
我有一个算法可以在数字列表中寻找好的对。一个好的配对被认为是索引 i 小于 j 且 arr[i] 1: # Finding the mid of the array
我从 API 收到一个 json,我需要解析并修改一个属性值。问题是,我收到的 json 数据的嵌套结构不一致,我无法控制它。 这将禁止我指定在特定深度(如 parsedJson.children[0
我有 451 个城市的坐标。现在我想计算每个城市之间的距离,然后根据该距离对一些结果进行排序。现在我有两个选择: 我可以运行一个循环来计算每个可能的城市组合的距离并将它们存储到一个表中,这将产生大约
对于返回相同结果的不同查询,我有两个查询计划我想知道是否有人可以告诉我哪个“更好”,以及为什么。 SELECT * FROM bids order by (select ranking from us
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
我有一个二维数组。我需要尽可能快地对其执行一些操作(函数每秒将被调用十几次,所以让它变得高效会很好)。 现在,假设我想获取元素 A[i][j],简单地使用 A[i][j] 在速度上有什么不同吗和 *(
在声明或使用字符串的代码中,我通常会看到开发人员这样声明它: string randomString = @"C:\Random\RandomFolder\ThisFile.xml"; 代替: str
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why don't CSS resets use '*' to cover all elements? 我正
如果我有一个包含许多重复项的 python 列表,并且我想遍历每个项目,而不是重复项,最好使用一个集合(如 set(mylist),或者找到另一种方法来创建没有重复的列表?我想只是循环遍历列表并检查重
在阅读常量接口(interface)反模式时,我发现没有实例的最终常量类比常量接口(interface)更好。 请解释一下怎么做? public interface ConstIfc { publ
我正在查看我继承的一些旧代码,我真的不喜欢某些地方的风格。我真的不喜欢它的外观的一件事是: bool func() { bool ret = true; ret &= test1();
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 Improv
我经常发现自己试图使用 boost/QT 信号解耦对象。实现这一点的简单方法是针对我要通信的每个具体类型,创建一个新的信号和插槽签名并连接所有相关对象。这导致了访问者模式,理想情况下我想发出一个访问者
我正在 https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html 上阅读有关 lambda 的内容 在方法
public List getInts() { List xs = new ArrayList(); xs.add(1); // return Collections.unmo
我是一名优秀的程序员,十分优秀!