Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Random;
- public class DuplicateCheck {
- public static final int TESTS = 20;
- public static final int CODES_PER_TEST = 1000000;
- public static void main(String[] args) {
- List<String> bigCodeList;
- Random random = new Random();
- long start = 0L;
- long result = 0L;
- long total1 = 0L;
- long total2 = 0L;
- for (int i = 0; i < TESTS; i++) {
- System.out.print("Creating code list... ");
- bigCodeList = new ArrayList<String>();
- for (int j = 0; j < CODES_PER_TEST; j++) {
- int a = 65 + random.nextInt(26);
- int b = 65 + random.nextInt(26);
- int c = 65 + random.nextInt(26);
- bigCodeList.add(new String(new char[] { (char) a, (char) b, (char) c }));
- }
- System.out.println("Done");
- System.out.print("Testing approach 1... ");
- start = System.currentTimeMillis();
- String tempStr = "";
- for (String aCode : bigCodeList) {
- if (tempStr.indexOf(aCode) == -1) {
- // deal With the aCode related work
- tempStr += aCode + "-";
- }
- }
- result = System.currentTimeMillis() - start;
- total1 += result;
- System.out.println("Done");
- System.out.println("Result: " + result + " ms");
- System.out.print("Testing approach 2... ");
- start = System.currentTimeMillis();
- HashSet<String> tempHSet = new HashSet<String>();
- for (String aCode : bigCodeList) {
- if (tempHSet.add(aCode)) {
- // deal With the aCode related work
- }
- }
- result = System.currentTimeMillis() - start;
- total2 += result;
- System.out.println("Done");
- System.out.println("Result: " + result + " ms");
- }
- System.out.println("Average for approach 1: " + Math.round(total1 / TESTS) + " ms");
- System.out.println("Average for approach 2: " + Math.round(total2 / TESTS) + " ms");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement