简体   繁体   中英

How fast does Java's JIT compiler work?

I've heard a lot about how JIT compilation makes code run faster than precompiled one when talking about "long-running" applications. But how long does it take to completely optimise your application?

Just for interest's sake, I'll show you example. I have some method, let's call it mySlowMethod() . It performs some matrix calculations such as rotation, multiplication and so on a few hundred times per second. I've measured a time of each call and got these results:

long time = System.nanoTime();
mySlowMethod();
System.out.println(System.nanoTime()-time);

//first call
1577187 (1.6 ms)
//next 2 seconds
~60000 (0.06 ms)
//later 
less than 10000 (0.01 ms)

The JIT actually operates in multiple smaller steps to minimize the performance impact on application start-up. These steps include:

  1. Multiple modes.

    • A baseline JIT compiler is used to perform a [relatively] simple direct conversion of bytecode to machine code for immediate execution of individual methods.
    • An optimizing compiler is used to perform many advanced optimizations for code that is executed many times, and/or is consuming a significant amount of computational resources.
  2. Incremental compilation.

    • While the JVM loads the bytecode of entire modules up front, the actual compilation process is often deferred until methods are actually executed for the first time. For large applications, such as an IDE, it's clear that not all features of the IDE are in use right when the application is launched, so this incremental compilation ability spreads out the JIT process over a much longer duration of application runtime to the point that it is often not even noticable.

Overall, the application is probably never "fully optimized", but you'd never know this because the parts you actually cared about were optimized early.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM