Is there a way to count the number of executed IL commands?

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

Edit I do not mean static analysis of the body of the method - I mean the actual number of commands executed - so if, for example, the body of the method includes a cycle, the count will be increased, however, many commands make up a cycle * the number of times the cycle repeats.

+4
source share
5 answers

I do not think that you can do what you want. This is because IL is only used during JIT (Just-In-Time) compilation. By the time the method was launched, IL was translated into native machine code. Thus, although it would be possible to calculate the number of IL instructions in a given method / type / assembly statically, there is no such concept at runtime.

You did not indicate your intention to know the number of IL instructions to be interpreted. Given that there is only some correlation between the number of IL-array of the method body and the actual number of machine code instructions, I do not see that knowing this number can achieve (except to satisfy your curiosity).

+4
source

Well, that won't be easy. I think you could build after compilation using the performance counter code that runs after the IL blocks. For example, if you had a section of a method that loaded int onto the stack, then you executed a static method using this int as part of optimized code, you could write a counter for the 2nd load and call.

Even using existing read / write projects using AI / managed assembly, this would be a pretty difficult task to take down.

Of course, some of the instructions that your counter wrote down can be optimized at compile time at compile time on x86 / ia64 / x64, but this is a risk you should take to try to profile based on abstract lanaguage as an IL.

+1
source

You can use ICorDebug , which has a managed interface. Grab a breakpoint at the beginning of the method and programmatically go through the code until it leaves the method.

However, I'm not sure how useful the metric will be, people tend to use time for these kinds of things. Some IL instructions are more expensive than others.

+1
source

I am using code metrics add in Reflector

Analysis of the CodeMetrics add-in and calculates several code quality metrics at your meetings. This add-on uses Reflector to compute classic metrics such as cyclomatic complexity or simpler metrics such as the number of local variables in a method. All results can be saved to a file.

Install the plugin. Select the build and boot method metric. It will show you a grid with CodeSize, CyclomaticComplexity, # instructions, etc.

0
source

I know that you do not want a static account. However, the static IL value for each arc, as well as the number of times the arc was executed together, gives you an IL value. To do this, you need to measure each arc, which requires the insertion of a counter.

(An arc is a sequence of instructions that you cannot enter or exit. If you execute the first command, you will always execute the last, etc.)

0
source

Source: https://habr.com/ru/post/1277432/


All Articles