<p>My professor did an informal benchmark on a little program and the Java times were: 1.7 seconds for the first run, and 0.8 seconds for the runs thereafter.  </p>

<ul>
<li><p>Is this due entirely to the loading of the runtime environment into the operating environment ?</p>

<p>OR </p></li>
<li><p>Is it influenced by Java's optimizing the code and storing the results of those optimizations (sorry, I don't know the technical term for that)?</p></li>
</ul>

[{'answer_id': 68124, 'author': 'Curt Hagenlocher', 'author_id': 533, 'author_profile': 'https://Stackoverflow.com/users/533', 'pm_score': 2, 'selected': False, 'text': "<p>I'm not aware of any virtual machine in widespread use that saves statistical usage data between program invocations -- but it certainly is an interesting possibility for future research.</p>\n\n<p>What you're seeing is almost certainly due to disk caching.</p>\n"}, {'answer_id': 68131, 'author': 'Cem Catikkas', 'author_id': 3087, 'author_profile': 'https://Stackoverflow.com/users/3087', 'pm_score': 0, 'selected': False, 'text': '<p>Java JVM (actually might change from different implementations of the JVM) when first started out will interpret the byte code. Once it detects that the code will be running enough number of times JITs it to native machine language so it runs faster. </p>\n'}, {'answer_id': 68199, 'author': 'Matt Gregory', 'author_id': 10577, 'author_profile': 'https://Stackoverflow.com/users/10577', 'pm_score': 3, 'selected': False, 'text': "<p>Okay, I found where I read that.  This is all from &quot;Learning Java&quot; (O'Reilly 2005):</p>\n<blockquote>\n<p>The problem with a traditional JIT compilation is that optimizing code takes time. So a JIT compiler can produce decent results but may suffer a significant latency when the application starts up. This is generally not a problem for long-running server-side applications but is a serious problem for client-side software and applications run on smaller devices with limited capabilities. To address this, Sun's compiler technology, called HotSpot, uses a trick called adaptive compilation. If you look at what programs actually spend their time doing, it turns out that they spend almost all their time executing a relatively small part of the code again and again. The chunk of code that is executed repeatedly may be only a small fraction of the total program, but its behavior determines the program's overall performance. Adaptive compilation also allows the Java runtime to take advantage of new kinds of optimizations that simply can't be done in a statically compiled language, hence the claim that Java code can run faster than C/C++ in some cases.</p>\n<p>To take advantage of this fact, HotSpot starts out as a normal Java bytecode interpreter, but with a difference: it measures (profiles) the code as it is executing to see what parts are being executed repeatedly. Once it knows which parts of the code are crucial to performance, HotSpot compiles those sections into optimal native machine code. Since it compiles only a small portion of the program into machine code, it can afford to take the time necessary to optimize those portions. The rest of the program may not need to be compiled at all—just interpreted—saving memory and time. In fact, Sun's default Java VM can run in one of two modes: client and server, which tell it whether to emphasize quick startup time and memory conservation or flat out performance.</p>\n<p>A natural question to ask at this point is, Why throw away all this good profiling information each time an application shuts down? Well, Sun has partially broached this topic with the release of Java 5.0 through the use of shared, read-only classes that are stored persistently in an optimized form. This significantly reduces both the startup time and overhead of running many Java applications on a given machine. The technology for doing this is complex, but the idea is simple: optimize the parts of the program that need to go fast, and don't worry about the rest.</p>\n</blockquote>\n<p>I'm kind of wondering how far Sun has gotten with it since Java 5.0.</p>\n"}, {'answer_id': 68602, 'author': 'big_peanut_horse', 'author_id': 10720, 'author_profile': 'https://Stackoverflow.com/users/10720', 'pm_score': 2, 'selected': False, 'text': "<p>I agree that it's likely the result of disk caching.</p>\n\n<p>FYI, the IBM Java 6 VM does contain an ahead-of-time compiler (AOT).  The code isn't quite as optimized as what the JIT would produce, but it is stored across VMs, I believe in some sort of persistent shared memory.  Its primary benefit is to improve startup performance.  The IBM VM by default JITs a method after it's been called 1000 times.  If it knows that a method is going to be called 1000 times just during the VM startup (think a commonly-used method like <code>java.lang.String.equals(...)</code> ), then it's beneficial for it to store that in the AOT cache so that it never has to waste time compiling at runtime.</p>\n"}, {'answer_id': 69412, 'author': 'Kevin Day', 'author_id': 10973, 'author_profile': 'https://Stackoverflow.com/users/10973', 'pm_score': 2, 'selected': True, 'text': '<p>I agree that the performance difference seen by the poster is most likely caused by disk latency bringing the JRE into memory.  The Just In Time compiler (JIT) would not have an impact on performance of a little application.</p>\n\n<p>Java 1.6u10 (<a href="http://download.java.net/jdk6/" rel="nofollow noreferrer">http://download.java.net/jdk6/</a>) touches the runtime JARs in a background process (even if Java isn\'t running) to keep the data in the disk cache.  This significantly decreases startup times (Which is a huge benefit to desktop apps, but probably of marginal value to server side apps).</p>\n\n<p>On large, long running applications, the JIT makes a big difference over time - but the amount of time required for the JIT to accumulate sufficient statistics to kick in and optimize (5-10 seconds) is very, very short compared to the overall life of the application (most run for months and months).  While storing and restoring the JIT results is an interesting academic exercise, the practical improvement is not very large (Which is why the JIT team has been more focused on things like GC strategies for minimizing memory cache misses, etc...).</p>\n\n<p>The pre-compilation of the runtime classes does help desktop applications quite a bit (as does the aforementioned 6u10 disk cache pre-loading).</p>\n'}, {'answer_id': 75199, 'author': 'eckes', 'author_id': 13189, 'author_profile': 'https://Stackoverflow.com/users/13189', 'pm_score': 1, 'selected': False, 'text': '<p>You should describe how your Benchmark was done. Especially at which point you start to measure the time. </p>\n\n<p>If you include the JVM startup time (which is useful for Benchmarking the User experience but not so useful to optimize Java code) then it might be a filesystem caching effect or it can be caused by a feature called "Java Class Data Sharing":</p>\n\n<p>For Sun:</p>\n\n<p><a href="http://java.sun.com/j2se/1.5.0/docs/guide/vm/class-data-sharing.html" rel="nofollow noreferrer">http://java.sun.com/j2se/1.5.0/docs/guide/vm/class-data-sharing.html</a></p>\n\n<p>This is an option where the JVM saves a prepared image of the runtime classes to a file, to allow quicker loading (and sharing) of those at the next start. You can control this with -Xshare:on or -Xshare:off with a Sun JVM. The default is -Xshare:auto which will load the shared classes image if present, and if not present it will write it at first startup if the directory is write able.</p>\n\n<p>With IBM Java 5 this is BTW even more powerful:</p>\n\n<p><a href="http://www.ibm.com/developerworks/java/library/j-ibmjava4/" rel="nofollow noreferrer">http://www.ibm.com/developerworks/java/library/j-ibmjava4/</a></p>\n\n<p>I don\'t know of any mainstream JVM which is saving JIT statistics.</p>\n'}]
['https://Stackoverflow.com/questions/68109', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10577/']