有没有一种方法来计算执行的IL指令数?方法来、指令、IL

2023-09-04 00:35:14 作者:日日思君不见君

我希望做一个C#程序的一些基准测试,但我不希望使用时间作为我的载体 - 我要计算,在一个特定的方法调用得到执行IL指令数。这可能吗?

I want to do some benchmarking of a C# process, but I don't want to use time as my vector - I want to count the number of IL instructions that get executed in a particular method call. Is this possible?

修改我说的不是方法体的静态分析 - 我指的指令被执行的实际数量 - 所以,如果,例如,该方法主体包括一个循环,计数将增加但许多指令补循环*的次数的循环重复

Edit I don't mean static analysis of a method body - I'm referring to the actual number of instructions that are executed - so if, for example, the method body includes a loop, the count would be increased by however many instructions make up the loop * the number of times the loop is iterated.

推荐答案

我不认为这是可以做到你想要什么。这是因为,IL仅JIT(刚刚在时间)编译期间使用。由当时的方法是运行IL已被翻译成母语机code。因此,虽然有可能算的IL指令数在给定的方法/类型/装配静态,有这样的概念在运行时。

I don't think it's possible to do what you want. This is because the IL is only used during JIT (Just-In-Time) compilation. By the time the method is running the IL has been translated into native machine code. So, while it might be possible to count the number of IL instructions in a given method/type/assembly statically, there's no concept of this at runtime.

您还没有规定在明知IL指令,这将是PTED跨$ P $数你的意图。假设有一个方法体的IL计数和机器code指令的实际数之间只有松散的关系,我看不出有什么知道这个数字将达到(除了满足你的好奇心)。

You haven't stated your intention in knowing the number of IL instructions that would be interpreted. Given that there's only a loose correlation between the IL count of a method body and the actual number of machine code instructions, I fail to see what knowing this number would achieve (other than satisfying your curiosity).