Skip to content

Instantly share code, notes, and snippets.

@emoseman
Created October 25, 2013 20:29
Show Gist options
  • Save emoseman/7161332 to your computer and use it in GitHub Desktop.
Save emoseman/7161332 to your computer and use it in GitHub Desktop.
Some tests for array search performance.
package com.aol.data.api.query;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import org.apache.commons.lang3.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.SecureRandom;
import java.util.Arrays;
/**
* Class Description TODO
*
* @author Evan Moseman (evan.moseman@corp.aol.com)
* @version 1.0
* @since 10/15/13
*/
public class DBUtilsTest {
private static final Logger log = LoggerFactory.getLogger(DBUtilsTest.class);
private static
//@formatter:off
int[] types = {2003,-5,-2,-7,2004,16,1,2005,70,91,3,2001,8,6,4,2000,-16,-4,-1,-15,2011,0,2,-9,1111,7,2006,-8,5,2009,2002,92,93,-6,-3,12};
int[] typesQuarterMisses = {2003,-5,-2,-7,2004,16,1,2005,70,91,3,2001,8,6,4,2000,-16,-4,-1,-15,2011,0,2,-9,1111,7,2006,-8,5,2009,2002,92,93,-6,-3,12,0,0,0,0,0,0,0,0,0};
int[] typesHalfMisses = {2003, -5, -2,-7,2004,16,1,2005,70,91,3,2001,8,6,4,2000,-16,-4,-1,-15,2011,0,2,-9,1111,7,2006,-8,5,2009,2002,92,93,-6,-3,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
// @Test
public void testperf()
throws Exception {
ImmutableList<Integer> A = ImmutableList.of(-15, -9, 1, 12, 91, 92, 93, 1111, 2002, 2009);
int[] B = {-15, -9, 1, 12, 91, 92, 93, 1111, 2002, 2009};
ImmutableMap<Integer, Integer>
C = ImmutableMap.<Integer, Integer>builder().put(-15, 0).put(-9, 0).put(1, 0).put(12, 0).put(91, 0).put(92, 0).put(93, 0).put(
1111, 0).put(2002, 0).put(2009, 0).build();
ImmutableSortedMap<Integer, Integer> D = ImmutableSortedMap.copyOf(C);
int n = 1000000;
int num;
SecureRandom sr = new SecureRandom();
StopWatch sw = new StopWatch();
sw.start();
for (int i = 0; i < n; i++) {
sw.suspend();
num = typesHalfMisses[sr.nextInt(typesHalfMisses.length)];
sw.resume();
A.contains(num);
}
sw.stop();
log.info("ImmutableList: {}", sw);
sw.reset();
sw.start();
for (int i = 0; i < n; i++) {
sw.suspend();
num = typesHalfMisses[sr.nextInt(typesHalfMisses.length)];
sw.resume();
C.containsKey(num);
}
sw.stop();
log.info("ImmutableMap: {}", sw);
sw.reset();
sw.start();
for (int i = 0; i < n; i++) {
sw.suspend();
num = typesHalfMisses[sr.nextInt(typesHalfMisses.length)];
sw.resume();
D.containsKey(num);
}
sw.stop();
log.info("ImmutableSortedMap: {}", sw);
sw.reset();
sw.start();
for (int i = 0; i < n; i++) {
sw.suspend();
num = typesHalfMisses[sr.nextInt(typesHalfMisses.length)];
sw.resume();
Arrays.binarySearch(B, num);
}
sw.stop();
log.info("Arrays.binarySearch: {}", sw);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment