All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liveramp.commons.collections.lightweight_trie.PerformanceTestGet Maven / Gradle / Ivy

/*
 *  Copyright 2013 Liveramp
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.liveramp.commons.collections.lightweight_trie;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class PerformanceTestGet {
  public static void main(String[] args) throws Exception {
    BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));

    List strings = new ArrayList();

    while (true) {
      String l = r.readLine();
      if (l == null) {
        break;
      }
      strings.add(l);
    }

    long hashMapCount = 0;
    long trieCount = 0;
    long immutableTrieCount = 0;

    Map hashMap = new HashMap();
    StringRadixTreeMap trieMap = new StringRadixTreeMap();

    for (int i = 0; i < strings.size(); i++) {
      hashMap.put(strings.get(i), i);
      trieMap.put(strings.get(i), i);
    }

    ImmutableStringRadixTreeMap optTrieMap = new ImmutableStringRadixTreeMap(trieMap);

//    final List nodeAnalysis = optTrieMap.getNodeAnalysis();
//    for (String s : nodeAnalysis) {
//      System.out.println(s);
//    }
//    System.exit(1);
    
    Collections.shuffle(strings, new Random(1));

    for (int trial = 0; trial < 50; trial++) {
      hashMapCount += perfTest("hashmap", hashMap, strings);
      trieCount += perfTest("trie", trieMap, strings);
      immutableTrieCount += perfTest("immutable trie", optTrieMap, strings);
    }

    System.out.println("hashmap\t" + hashMapCount);
    System.out.println("trie\t" + trieCount);
    System.out.println("i-trie\t" + immutableTrieCount);
  }

  private static long perfTest(String title, Map map, List strings) {
    System.gc();System.gc();System.gc();System.gc();
    long start = System.currentTimeMillis();
    for (int i = 0; i < strings.size(); i++) {
      final String s = strings.get(i);
      map.get(s);
    }
    long end = System.currentTimeMillis();
    return (end - start);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy