Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using Java's system time is not a good way to get a real benchmark. I refactored your code to use <a href="http://code.google.com/p/caliper/" rel="nofollow">Google Caliper</a> (which warms up the JVM, among other things)... and discovered similar results to you. Commenters correctly pointed out my original version was not good and that most of the time was going to <code>System.out.println</code> calls.</p> <p>Like I said, writing a benchmark is hard. Updated below is the new, correct version.</p> <h2>Results:</h2> <pre><code> 0% Scenario{vm=java, trial=0, benchmark=HashMap} 51.04 ns; σ=0.22 ns @ 3 trials 50% Scenario{vm=java, trial=0, benchmark=Array} 4.05 ns; σ=0.01 ns @ 3 trials benchmark ns linear runtime HashMap 51.04 ============================== Array 4.05 == </code></pre> <h2>Code:</h2> <pre><code>import com.google.caliper.Runner; import com.google.caliper.SimpleBenchmark; public class PerfTest { public static double hashNum = 0; public static double arrayNum = 0; public static class PerfBenchmark extends SimpleBenchmark { static java.util.HashMap&lt;String, Double&gt; map; static Double[] array = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0}; static{ map = new java.util.HashMap&lt;String, Double&gt;(); map.put("1", new Double(1)); map.put("2", new Double(2)); map.put("3", new Double(3)); map.put("4", new Double(4)); map.put("5", new Double(5)); map.put("6", new Double(6)); map.put("7", new Double(7)); map.put("8", new Double(8)); map.put("9", new Double(9)); map.put("10", new Double(10)); } public void timeHashMap(int nTimes){ int sz = map.size(); String key = "1"; for (int i=0; i &lt;nTimes; i++){ double sum = 0; for (int j =1; j&lt;=sz; j++){ sum += map.get(key); } hashNum += sum; } } public void timeArray(int nTimes){ for (int i=0; i &lt;nTimes; i++){ double sum = 0; for (int j=0; j&lt; array.length; j++) { sum += array[j]; } arrayNum += sum; } } } public static void main(String[] args){ Runner.main(PerfBenchmark.class, new String[0]); System.out.println(hashNum); System.out.println(arrayNum); } } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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