Can I compare IL code to determine which technique is faster or better?

[亡魂溺海] 提交于 2019-12-05 08:19:17
  • 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) Your assumptions are correct about what's happening.

2) You need to understand what the IL code is doing in order to determine which is "better"

3) No. It means it takes fewer instructions to run. However, these individual instructions might use more memory or less. For example, the instruction you were referencing, in one case is creating a Func delegate, and in the other is creating a Converter object. Without more information, it's difficult to tell which of those two things is more expensive.

4) Yes and no....

The problem is that the IL code will tell you what's happening, but it's really the nested calls in IL that are going to be the large performance driver. If the IL code is doing simple operations everywhere, in general the shorter the better (although individual IL operations can vary in speed, themselves). When the code calls into methods or constructors on other types, such as yours, this becomes impossible to tell from this alone. One line of IL can take longer in one case (if it's calling an expensive method, for example) than 50 in another case (where they're doing simple operations).

In your case above, for example, the first 20 operations are very, very fast, wheras the last few take nearly all of your executable time.

The chunk of the work for both answers is done at IL 004A (and IL 004F for the second one). Unless you know the cost of these external calls, there's no practical basis on which you could compare the performance of the two answers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!