Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another way you might be able to improve the precision of your measurements is:</p> <ul> <li>Do your calculations in nano-seconds, so elapsed time (using <code>System.nanoTime()</code> and CPU time are natively in nanos. Blocked and Waiting times should be converted since they are in millis.</li> <li>Implement an approximation of how long it takes to simply capture elapsed, cpu, blocked and waiting times. (Call this OVERHEAD.) You might do this in a static block somewhere. Do a JVM warmup loop first, keeping in mind that the JIT kicks in after 15000 method calls [by default ? depends on your JVM....], so a warmup loop of at least that size would be good. </li> <li>The run the post-warmup loop, take the total elapsed divided by the loop count and you will have your OVERHEAD value. </li> <li>Subtract OVERHEAD from your calculated method elapsed times to get a better idea of the code execution time of the method, or....</li> <li>Add OVERHEAD to your addition of CPU, Blocked and Waited to get closer to the actual wall time.</li> <li>Optionally recalculate OVERHEAD from time to time.</li> </ul> <p>It's not perfect, and it is certainly not exacting, but it might give you slightly better numbers.</p> <p>Here's some test code I ran to calculate my average OVERHEAD (Java 7, Windows 7, 64 bit). I tried to make sure no methods were being ellided, but your mileage may vary.</p> <pre><code>public class Overhead { static final ThreadMXBean tmx = ManagementFactory.getThreadMXBean(); public static void main(String[] args) { tmx.setThreadContentionMonitoringEnabled(true); tmx.setThreadCpuTimeEnabled(true); int loops = 15000; long sum = -1; long start = System.nanoTime(); for(int i = 0; i &lt; loops; i++) { sum = measure(); } long elapsed = System.nanoTime()-start; log("Warmup completed in [" + elapsed + "] ns. "); log("Sum:" + sum); start = System.nanoTime(); loops = loops * 2; for(int i = 0; i &lt; loops; i++) { sum = measure(); } elapsed = System.nanoTime()-start; long avg = (elapsed/loops); log("Test completed in [" + elapsed + "] ns. OVERHEAD: [" + avg + "] ns."); log("Sum:" + sum); } protected static long measure() { long s1 = System.nanoTime(); long bt = tmx.getCurrentThreadCpuTime(); ThreadInfo ti = tmx.getThreadInfo(Thread.currentThread().getId()); long blocked = ti.getBlockedTime(); long waited = ti.getWaitedTime(); long s2 = System.nanoTime(); return ((s2 - s1) + blocked + waited + bt); } public static void log(Object msg) { System.out.println(msg); } } </code></pre> <p>My output was as follows:</p> <pre><code>Overhead test Warmup completed in [43176164] ns. Sum:109201929 Test completed in [38482368] ns. OVERHEAD: [1282] ns. Sum:156002228 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload