Note that there are some explanatory texts on larger screens.

plurals
  1. POJVM Internals - Why does the same piece of code runs in different timings?
    primarykey
    data
    text
    <p>I am trying to understand the JVM options and came across the option <code>CompileThreshold</code>. I am running the below program:</p> <pre><code>public class Main { public static void main (String [] args) { for (int repeat = 0; repeat &lt; 200; ++ repeat) { long start = System.nanoTime(); sum (100); long end = System.nanoTime(); long diff = end - start; System.out.println (repeat + ": " + diff); } } ` public static int sum (int n) { if (n &lt;= 1) return 1; else return n + sum (n - 1); } } </code></pre> <p>I get the below output:</p> <pre><code>0: 8555 1: 6416 2: 6416 3: 5988 4: 6416 5: 8555 6: 5989 7: 6416 8: 6416 9: 5988 10: 5561 11: 5988 12: 5988 13: 5988 14: 5988 15: 5988 16: 5988 17: 5988 18: 5988 19: 6416 20: 5988 21: 5988 22: 5988 23: 5988 24: 5560 25: 6416 26: 5988 27: 5561 28: 5560 29: 5989 30: 5989 31: 5988 32: 21814 33: 6416 34: 6843 35: 6416 36: 6844 37: 6416 38: 6416 39: 6844 40: 6416 41: 6415 42: 6416 43: 5988 44: 6416 45: 6844 46: 6416 47: 6416 48: 6416 49: 6416 50: 6416 51: 6416 52: 5988 53: 6416 54: 6844 55: 6843 56: 6416 57: 6844 58: 6416 59: 6416 60: 6415 61: 6416 62: 6416 63: 6416 64: 5988 65: 5988 66: 6416 67: 6416 68: 6844 69: 6416 70: 6416 71: 6416 72: 6416 73: 6416 74: 6415 75: 6416 76: 6416 77: 6416 78: 6416 79: 6415 80: 6844 81: 5988 82: 6415 83: 6416 84: 6416 85: 6416 86: 6416 87: 6416 88: 7272 89: 6416 90: 6416 91: 6844 92: 6844 93: 6415 94: 6416 95: 6415 96: 6415 97: 6416 98: 6416 99: 25236 100: 6416 101: 6416 102: 6843 103: 6416 104: 5988 105: 6416 106: 6415 107: 6416 108: 6416 109: 6416 110: 6416 111: 6416 112: 6416 113: 6844 114: 6416 115: 6416 116: 6416 117: 5988 118: 6416 119: 6416 120: 6416 121: 6415 122: 6416 123: 6415 124: 6416 125: 6416 126: 6416 127: 6416 128: 5988 129: 5988 130: 6416 131: 6416 132: 5988 133: 5988 134: 6416 135: 6416 136: 5989 137: 6416 138: 6416 139: 5988 140: 5988 141: 6416 142: 5560 143: 5988 144: 6416 145: 6843 146: 6415 147: 6416 148: 6843 149: 11549 150: 6416 151: 6844 152: 6416 153: 6415 154: 6416 155: 5988 156: 5988 157: 6416 158: 6416 159: 6416 160: 6416 161: 6415 162: 6416 163: 6416 164: 6416 165: 6416 166: 5988 167: 5988 168: 5988 169: 6416 170: 6416 171: 6416 172: 5988 173: 5988 174: 6844 175: 5988 176: 6416 177: 6416 178: 6844 179: 6416 180: 6415 181: 6416 182: 5988 183: 5989 184: 6416 185: 6416 186: 6416 187: 6416 188: 5989 189: 5560 190: 5988 191: 6416 192: 6415 193: 6416 194: 18820 195: 1711 196: 1284 197: 2139 198: 1711 199: 1711 </code></pre> <p>If you observe, there are spikes in the times at 32, 99, 149, 194.<BR></p> <p>After 194, the time taken to run is comparitively less. Please explain this behaviour. Thanks.</p>
    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.
 

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