Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.google.common.collect.Sets;
- import it.unimi.dsi.fastutil.ints.IntArraySet;
- import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
- import it.unimi.dsi.fastutil.ints.IntSet;
- import org.apache.commons.lang3.RandomUtils;
- import java.util.BitSet;
- import java.util.HashSet;
- public class Main{
- public static void main(String[] args) throws Exception {
- int[] ints = new int[10000];
- for (int i = 0; i < 10000; i++) {
- ints[i] = RandomUtils.nextInt(0, 10000);
- }
- int[] check = new int[10000];
- for (int i = 0; i < 10000; i++) {
- check[i] = RandomUtils.nextInt(0, 10000);
- }
- for (int i = 0; i < 10; i++) {
- System.out.println("=============================================");
- testHashSet(ints, check);
- testHashSet(ints, check);
- testIntSet(ints, check);
- testIntSet(ints, check);
- testArray(ints, check);
- testArray(ints, check);
- testBitSet(ints, check);
- testBitSet(ints, check);
- // testIntArraySet(ints, check);
- // testIntArraySet(ints, check);
- }
- }
- public static void testHashSet(int[] ints, int[] check) {
- HashSet<Integer> set = Sets.newHashSetWithExpectedSize(ints.length);
- for (int i : ints) {
- set.add(i);
- }
- long time = System.currentTimeMillis();
- for (int i = 0; i < 5000; i++) {
- for (int j = 0; j < check.length; j++) {
- set.contains(check[j]);
- }
- }
- System.out.println("hashset " + (System.currentTimeMillis() - time) + "ms");
- }
- public static void testIntSet(int[] ints, int[] check) {
- IntSet set = new IntOpenHashSet(capacity(ints.length));
- for (int i : ints) {
- set.add(i);
- }
- long time = System.currentTimeMillis();
- for (int i = 0; i < 5000; i++) {
- for (int j = 0; j < check.length; j++) {
- set.contains(check[j]);
- }
- }
- System.out.println("intset " + (System.currentTimeMillis() - time) + "ms");
- }
- public static void testIntArraySet(int[] ints, int[] check) {
- IntSet set = new IntArraySet(capacity(ints.length));
- for (int i : ints) {
- set.add(i);
- }
- long time = System.currentTimeMillis();
- for (int i = 0; i < 5000; i++) {
- for (int j = 0; j < check.length; j++) {
- set.contains(check[j]);
- }
- }
- System.out.println("intarrayset " + (System.currentTimeMillis() - time) + "ms");
- }
- public static void testArray(int[] ints, int[] check) {
- boolean[] booleans = new boolean[ints.length];
- for (int i : ints) {
- booleans[i] = true;
- }
- long time = System.currentTimeMillis();
- for (int i = 0; i < 5000; i++) {
- for (int j = 0; j < check.length; j++) {
- boolean aBoolean = booleans[j];
- }
- }
- System.out.println("array boolean[] " + (System.currentTimeMillis() - time) + "ms");
- }
- public static void testBitSet(int[] ints, int[] check) {
- BitSet bitSet = new BitSet(ints.length);
- for (int i : ints) {
- bitSet.set(i);
- }
- long time = System.currentTimeMillis();
- for (int i = 0; i < 5000; i++) {
- for (int j = 0; j < check.length; j++) {
- boolean aBoolean = bitSet.get(j);
- }
- }
- System.out.println("bitset " + (System.currentTimeMillis() - time) + "ms");
- }
- static int capacity(int expectedSize) {
- if (expectedSize < 3) {
- return expectedSize + 1;
- } else {
- return expectedSize < 1073741824 ? (int)((float)expectedSize / 0.75F + 1.0F) : 2147483647;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement