(How) does the Java JIT compiler optimize my code?

前端 未结 4 1567
自闭症患者
自闭症患者 2020-12-29 07:48

I\'m writing fairly low level code that must be highly optimized for speed. Every CPU cycle counts. Since the code is in Java I can\'t write as low level as in C for example

相关标签:
4条回答
  • 2020-12-29 07:50

    I've done a lot of performance code in Java, I've even coded directly in Bytecode, enough to be sure of a couple of thing : the JIT is a black box with obscure behaviours, the JIT and compilers are incredibly efficient, and the simplest code usually yield the best performance.

    This is normal when you think about the GOAL of the JIT: extract the best possible performance from any Java code. When you add that Java is quite a simple and plain language, the simple code will be optimized, and any further trick will generally do no good.

    Of course, there are some common pitfalls and gotchas you ought to know, but I see none in your code samples. Were I to optimize your code, I would go straight to the higher level: algorithm. What is the complexity of your code? Can some data be cached? What APIs are used? Etc... There's a seemingly endless pit of performance to be extracted from algorithmic tricks alone.

    And if even this is not sufficient, if the language is not fast enough, if your machine is not fast enough, if your algorithm cannot be made any faster, the answer won't lie in "clock cycles", because you might squeeze 20% of efficiency, but 20% will never be enough when your data grow. To be sure you will never hit (again) a performance wall, the ultimate answer lies in scalability: make your algorithm and your data endlessly distributable so you can just throw more workers to the task.

    0 讨论(0)
  • 2020-12-29 08:10

    How do you test the performance?

    Here is a good article:

    http://www.ibm.com/developerworks/java/library/j-benchmark1/index.html

    http://www.ibm.com/developerworks/java/library/j-benchmark2/index.html

    http://ellipticgroup.com/html/benchmarkingArticle.html

    0 讨论(0)
  • 2020-12-29 08:12

    You don't need to (& 0xff) before shifting 24 bits to the left.

    0 讨论(0)
  • 2020-12-29 08:14

    I do agree with solendil, but if you want to dig deeper at the low-level, try getting the code produced by the JIT as described here.

    0 讨论(0)
提交回复
热议问题