0

So I'm still fairly new to programming and I'm just wondering if I'm doing these benchmarks correctly. For queue I'm basically giving it a list filled with integers, and I would time how long it would take for it to find a number on the list. As for the HashMap it's basically the same idea, I would time how long it would take to get a number from the list. Also for both of them I would also time how long it would take for them to remove the contents of the list. Any help on this would be appreciated. Thank you!

// Create a queue, and test its performance
PriorityQueue<Integer> queue = new PriorityQueue <> (list);
System.out.println("Member test time for Priority Queue is " +
        getTestTime(queue) + " milliseconds");
      System.out.println("Remove element time for Priority Queue is " +
        getRemoveTime(queue) + " milliseconds");

// Create a hash map, and test its performance
HashMap<Integer, Integer> newmap = new HashMap<Integer, Integer>();
 for (int i = 0; i <N;i++) {
  newmap.put(i, i);
}
System.out.println("Member test time for hash map is " +
        getTestTime1(newmap) + " milliseconds");
 System.out.println("Remove element time for hash map is " +
        getRemoveTime1(newmap) + " milliseconds");
}



public static long getTestTime(Collection<Integer> c) {
  long startTime = System.currentTimeMillis();

 // Test if a number is in the collection
 for (int i = 0; i < N; i++)
   c.contains((int)(Math.random() * 2 * N));

return System.currentTimeMillis() - startTime; 
}

 public static long getTestTime1(HashMap<Integer,Integer> newmap) {
   long startTime = System.currentTimeMillis();

     // Test if a number is in the collection
     for (int i = 0; i < N; i++)
      newmap.containsKey((int)(Math.random() * 2 * N));

   return System.currentTimeMillis() - startTime; 
 }

public static long getRemoveTime(Collection<Integer> c) {
 long startTime = System.currentTimeMillis();

  for (int i = 0; i < N; i++)
   c.remove(i);

 return System.currentTimeMillis() - startTime; 
}
public static long getRemoveTime1(HashMap<Integer,Integer> newmap) {
    long startTime = System.currentTimeMillis();

     for (int i = 0; i < N; i++)
       newmap.remove(i);

      return System.currentTimeMillis() - startTime; 
   }
}
Juan.H
  • 15
  • 4
  • Don't write time measurements for benchmarks by hand, it's really not that easy to do it right. Use something like JMH: http://openjdk.java.net/projects/code-tools/jmh/ – Roman Puchkovskiy Dec 05 '17 at 18:55
  • Search SO for your problem before asking a question. The question below has a lot of good stuff in the answers: https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – mhvelplund Dec 05 '17 at 19:41

1 Answers1

0

I have two suggestion. First, when doing benchmarking, do the bare minimum work immediately before and after the code you are evaluating. You don't want your benchmark activity to affect the result.

Second, System.currentTimeMillis() can, depending on the OS, only be accurate within 10 milliseconds. Better to use System.nanoTime(), which is accurate to perhaps 200 nanoseconds. Divide by 1_000_000 to get milliseconds.

Practically,

final long startNanos, endNanos;
startNanos = System.nanoTime();
// your code goes here
endNanos = System.nanoTime();
// display the results of the benchmark
Steve11235
  • 2,849
  • 1
  • 17
  • 18