Advertisement
Pietu1998

Duplicate checking performance test

Jun 6th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashSet;
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. public class DuplicateCheck {
  7.    
  8.     public static final int TESTS = 20;
  9.     public static final int CODES_PER_TEST = 1000000;
  10.  
  11.     public static void main(String[] args) {
  12.         List<String> bigCodeList;
  13.         Random random = new Random();
  14.        
  15.         long start = 0L;
  16.         long result = 0L;
  17.         long total1 = 0L;
  18.         long total2 = 0L;
  19.        
  20.         for (int i = 0; i < TESTS; i++) {
  21.             System.out.print("Creating code list... ");
  22.             bigCodeList = new ArrayList<String>();
  23.             for (int j = 0; j < CODES_PER_TEST; j++) {
  24.                 int a = 65 + random.nextInt(26);
  25.                 int b = 65 + random.nextInt(26);
  26.                 int c = 65 + random.nextInt(26);
  27.                 bigCodeList.add(new String(new char[] { (char) a, (char) b, (char) c }));
  28.             }
  29.             System.out.println("Done");
  30.  
  31.             System.out.print("Testing approach 1... ");
  32.             start = System.currentTimeMillis();
  33.             String tempStr = "";
  34.             for (String aCode : bigCodeList) {
  35.                 if (tempStr.indexOf(aCode) == -1) {
  36.                     // deal With the aCode related work
  37.                     tempStr += aCode + "-";
  38.                 }
  39.             }
  40.             result = System.currentTimeMillis() - start;
  41.             total1 += result;
  42.             System.out.println("Done");
  43.             System.out.println("Result: " + result + " ms");
  44.  
  45.             System.out.print("Testing approach 2... ");
  46.             start = System.currentTimeMillis();
  47.             HashSet<String> tempHSet = new HashSet<String>();
  48.             for (String aCode : bigCodeList) {
  49.                 if (tempHSet.add(aCode)) {
  50.                     // deal With the aCode related work
  51.                 }
  52.             }
  53.             result = System.currentTimeMillis() - start;
  54.             total2 += result;
  55.             System.out.println("Done");
  56.             System.out.println("Result: " + result + " ms");
  57.         }
  58.  
  59.         System.out.println("Average for approach 1: " + Math.round(total1 / TESTS) + " ms");
  60.         System.out.println("Average for approach 2: " + Math.round(total2 / TESTS) + " ms");
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement