How to measure the effectiveness (in particular: assembly code) for java programs?

I asked questions, the answers to which often came to me in the direction of efficiency. Instead of continuing to ask such questions, I would rather look at efficiency. However, of course, there are many different types of effectiveness. The number of lines in the code, assembly, the time it takes to write, the time it takes for the computer to execute. And I probably missed a lot more ways to look at this.

Since there are so many ways to look at efficiency, I would like to start with , how do you get the assembly code of your own code and stop at the right place inside your code? (Let’s say you have 500 lines of code and you are interested in line 450-460). I'm not sure if this is already a function found in the IDE, if so, how can I find such functions? (= What are the names of such things so that I can find it for my own IDEs if it has this function).

(There is a lot of information about efficiency in Stack Overflow, I looked at many of them, but it was difficult for me to understand that people often start by analyzing their code. Their code, then the assembly, or they just use the IDE to find the most expensive methods and change them in this way? This is a bad question because it, of course, depends on many factors, but I hope to get at least a little more feel it, and I think the answer to this question can help.)

+5
source share
2 answers

You are unlikely to get anything useful for parsing the assembly code generated by the JVM. If you are worried about performance, this is the wrong approach. You cannot talk about high performance by measuring low-level parts. This probably worked well for the first programs, where you could calculate the loops manually. Computers are much more complex these days - especially when you use a high-level language such as Java. The following must be considered:

  • The garbage collector stops
  • JIT with various optimizations
  • Several levels of command and data caches.
  • Prediction of branches.

Those mechanisms and the interactions between them are not something you can easily predict. There is only one normal way to write fast programs that I know:

  • Write a part of the program.
  • Determine that it is really too slow.
  • Identify and correct the neck of the bottle.
  • Go to # 1.

To identify bottle necks, I usually start with JVisualVM and, if necessary, start using lower level tools:

In 99% of cases, the problem is a slow algorithm, incorrect data structure, inefficient access to the disk, or something silly, like in debug logs. Microoptimizations such as avoiding unneeded copies or memory allocation will help less often. In rare cases, if all else fails, and you need to compress several loops from some narrow loop, it might be worth looking at the generated assembly. PrintAssembly may be useful in this case. You want to start with tools that are likely to help you.

+4
source

To get the JVM assembly code, you can use the PrintAssembly parameter. It gives readable code with comments pointing to lines in java sources. This way you can clearly understand how your Java code is compiled. But I think that looking at the processor instructions is very difficult to understand the neck of the bottle. For this, I used VisualVM and JProfiler.

+2
source

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


All Articles