vendredi 8 novembre 2019

Generate 7 million random strings that do not overlap

i tried to Generate 7 million random strings that do not overlap.

However, my code took 28023 ms to generate 7 million non-overlapping strings.

I don't know how to efficiently generate 7 million strings without overlap.

i tried to use hashmap, list, ... because I need a key-value type

    HashMap<String, Integer> map = new HashMap();
    long start = System.currentTimeMillis();

    for(int i = 0 ; i < 7000000 ; ) {
        int MAX_LENGTH = 4;
        int MIN_LENGTH = 2;

        StringBuffer temp = new StringBuffer();
        Random rnd = new Random();
        int length = rnd.nextInt(MAX_LENGTH - MIN_LENGTH + 1) + MIN_LENGTH; // 문자열 길이 랜덤(2~4자리)

        for (int j = 0; j < length; j++) {
            int rIndex = rnd.nextInt(2);
            switch (rIndex) {
                case 0:
                    // a-z(소문자)
                    temp.append((char) ((int) (rnd.nextInt(26)) + 97));
                    break;
                case 1:
                    // A-Z(대문자)
                    temp.append((char) ((int) (rnd.nextInt(26)) + 65));
                    break;
            }
        }


        String str = temp.toString();

        if(!map.containsKey(str)) {
            map.put(str, rnd.nextInt());
            i++;
        }
    }

    long end = System.currentTimeMillis();

    System.out.println("Setup Performance : " + (end - start));

my code took 28023 ms to generate 7 million non-overlapping strings.




Aucun commentaire:

Enregistrer un commentaire