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.
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.
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).