Search code examples
javajit

A concrete Example of the effect of the JIT in java


So I am aware the java has just in time compilation (the JIT), which gives it an advantage over statically compiled languages like C++. Are there any examples illustrating the java JIT? Possible examples could be outperforming C or C++ code for a given algorithm? Or showing an algorithm's iterations getting faster with time (I am unsure if that would be an instance of the JIT). Or just any example which can show some sort of measurement of the existence of the JIT doing this? I ask this question because I have only ever read about the JIT and wish to prove it's existence as opposed to just believing in it like some sort of religious God.

Remark - If this question is too opinionated please comment and let me know why. I am just curious about the JIT and after using java for a few years still to this day am unaware of how I benefit from it, and if it lives up to the hype of outperforming its statically compiled counterparts.

Additional Information - I have read about when it does it, and am not looking for more information I will just need to believe is true, I want to see something which shows me doing what it is suppose to do.

EDIT - Good that I have allot of responses, what has been said is that comparing speed alone JIT optimised vs. C++ is not a good approach, and that a pure java comparison would be the least horrible. What about an example showing this with java:

Possible profile of example

So a JIT and Non-JIT optimised program doing the same are executed. At the start the JIT has not kicked in, and the program begins getting quicker whilst the static always has the same performance. Then the conditions change at 5.5 seconds or so and the application is being used slightly differently. The JIT has the ability to adapt to these changes again, firstly the time spikes and then it begins optimising again and can even reach a better optima becaue the application is being used slightly different. Would this be an acceptable example to show a JIT? (I will endevour to achieve this and review everyones links and videos).


Solution

  • I do not think you can convincingly prove that java using JIT is faster than C/C++ statically compiled code.

    You could find some code in java that beat its c/c++ implementation. For that you need to search for keywords like (benchmark,Java,JIT,C,C++ )

    I have purposely not mentioned any code or links for the above because of my point below.

    Most of the times people show java code beating statically compiled c/c++ in following ways

    1. Find part where java is fast compared to c/c++(memory allocation)and write only code to highlight it.
    2. Find weak points of c/C++ code and try to write java code that beat the c/c++ code in achieving the result.
    3. Run code in environment where you have advantage like having fast hardware and good amount of memory .

    My point being you are trying to find exception where java is faster that C/C++ and then generalizing it to the whole language. You could easily find more more examples of c/c++ beating java code just by using pointer in many algorithm.

    Such code benchmark testing is of no value in real life application development.

    Summarizing ( in real life application development )

    1. Java was slow compared to c/c++ when it first came out. But in the past decade improvements made in JVM coupled with JIT,Hotspot etc have made java as good as C/C++. Java is not slow nowadays. But I would not call it fast over c/c++. Any difference in real life application development in negligible because of language improvement as well as better hardware.

    2. You cannot generalize that java is faster than c/c++ by beating it one time in a particular environment with a particular algorithm or code.

    You might find some interesting info in the following links

    1. https://softwareengineering.stackexchange.com/questions/110634/why-would-it-ever-be-possible-for-java-to-be-faster-than-c

    2. Is Java really slow?

    Since question has been edited to now try and find the performance improvement of using JIT , I am editing my answer to add a few more points.

    My understanding of JIT is that it improves the code that is most executed , to a version that can be run really fast by compiler. Most of the examples of JIT optimisation techniques I have come across shows actions which could also be done by the programmer but then would affect the readability of the program or may not confirm to the framework or coding styles the programmer is/has to use.

    So what I am trying to say here is if you write a program that can be improved by JIT it will do so and you will see an increase in performace. But if you are someone who understand JVM and write java code that is already optimized then JIT may not give you much benefit.

    So in effect if you see a performace improvement when running a program using JIT that improvement is not guaranteed for all java programs. It depends on the program.

    These links below show some JIT improvements using code examples.

    http://www.infoq.com/articles/Java-Application-Hostile-to-JIT-Compilation

    https://plumbr.eu/blog/java/do-you-get-just-in-time-compilation

    Anyway if we need to to differentiate the performance while using JIT, we would run a java program with JIT enabled and run the same program again with JIT disabled.

    This link http://www.javacodegeeks.com/2013/07/java-just-in-time-compilation-more-than-just-a-buzzword.html has a case study on this topic and recommends the following

    Assessing the JIT benefits for your application

    In order to understand the impact of not using JIT for your Java application, I recommend that you preform the following experiment:

    1. Generate load to your application with JIT enabled and capture some baseline data such as CPU %, response time, # requests etc

    2. Disable JIT

    3. Redo the same testing and compare the results.

    This link http://people.cse.iitd.ac.in/~sbansal/csl862-virt/readings/CompileJava97.pdf does benchmark JIT and shows speed improvements over basic JVM interpretations.

    To understand what JIT does to your code , you could use the tool JITwatch.

    https://github.com/AdoptOpenJDK/jitwatch

    The links below explain its utility.

    http://www.oracle.com/technetwork/articles/java/architect-evans-pt1-2266278.html

    http://zeroturnaround.com/rebellabs/why-it-rocks-to-finally-understand-java-jit-with-jitwatch/