Advertisement
Lokha

Untitled

Dec 14th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. import com.google.common.collect.Sets;
  2. import it.unimi.dsi.fastutil.ints.IntArraySet;
  3. import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
  4. import it.unimi.dsi.fastutil.ints.IntSet;
  5. import org.apache.commons.lang3.RandomUtils;
  6.  
  7. import java.util.BitSet;
  8. import java.util.HashSet;
  9.  
  10. public class Main{
  11.  
  12. public static void main(String[] args) throws Exception {
  13. int[] ints = new int[10000];
  14. for (int i = 0; i < 10000; i++) {
  15. ints[i] = RandomUtils.nextInt(0, 10000);
  16. }
  17.  
  18. int[] check = new int[10000];
  19. for (int i = 0; i < 10000; i++) {
  20. check[i] = RandomUtils.nextInt(0, 10000);
  21. }
  22.  
  23.  
  24. for (int i = 0; i < 10; i++) {
  25. System.out.println("=============================================");
  26. testHashSet(ints, check);
  27. testHashSet(ints, check);
  28. testIntSet(ints, check);
  29. testIntSet(ints, check);
  30. testArray(ints, check);
  31. testArray(ints, check);
  32. testBitSet(ints, check);
  33. testBitSet(ints, check);
  34. // testIntArraySet(ints, check);
  35. // testIntArraySet(ints, check);
  36. }
  37. }
  38.  
  39. public static void testHashSet(int[] ints, int[] check) {
  40. HashSet<Integer> set = Sets.newHashSetWithExpectedSize(ints.length);
  41. for (int i : ints) {
  42. set.add(i);
  43. }
  44.  
  45. long time = System.currentTimeMillis();
  46. for (int i = 0; i < 5000; i++) {
  47. for (int j = 0; j < check.length; j++) {
  48. set.contains(check[j]);
  49. }
  50. }
  51. System.out.println("hashset " + (System.currentTimeMillis() - time) + "ms");
  52. }
  53.  
  54. public static void testIntSet(int[] ints, int[] check) {
  55. IntSet set = new IntOpenHashSet(capacity(ints.length));
  56. for (int i : ints) {
  57. set.add(i);
  58. }
  59.  
  60. long time = System.currentTimeMillis();
  61. for (int i = 0; i < 5000; i++) {
  62. for (int j = 0; j < check.length; j++) {
  63. set.contains(check[j]);
  64. }
  65. }
  66. System.out.println("intset " + (System.currentTimeMillis() - time) + "ms");
  67. }
  68.  
  69. public static void testIntArraySet(int[] ints, int[] check) {
  70. IntSet set = new IntArraySet(capacity(ints.length));
  71. for (int i : ints) {
  72. set.add(i);
  73. }
  74.  
  75. long time = System.currentTimeMillis();
  76. for (int i = 0; i < 5000; i++) {
  77. for (int j = 0; j < check.length; j++) {
  78. set.contains(check[j]);
  79. }
  80. }
  81. System.out.println("intarrayset " + (System.currentTimeMillis() - time) + "ms");
  82. }
  83.  
  84. public static void testArray(int[] ints, int[] check) {
  85. boolean[] booleans = new boolean[capacity(ints.length)];
  86. for (int i : ints) {
  87. booleans[i] = true;
  88. }
  89.  
  90. long time = System.currentTimeMillis();
  91. for (int i = 0; i < 5000; i++) {
  92. for (int j = 0; j < check.length; j++) {
  93. boolean aBoolean = booleans[check[j]];
  94. }
  95. }
  96. System.out.println("array boolean[] " + (System.currentTimeMillis() - time) + "ms");
  97. }
  98.  
  99. public static void testBitSet(int[] ints, int[] check) {
  100. BitSet bitSet = new BitSet(ints.length);
  101. for (int i : ints) {
  102. bitSet.set(i);
  103. }
  104.  
  105. long time = System.currentTimeMillis();
  106. for (int i = 0; i < 5000; i++) {
  107. for (int j = 0; j < check.length; j++) {
  108. boolean aBoolean = bitSet.get(check[j]);
  109. }
  110. }
  111. System.out.println("bitset " + (System.currentTimeMillis() - time) + "ms");
  112. }
  113.  
  114. static int capacity(int expectedSize) {
  115. if (expectedSize < 3) {
  116. return expectedSize + 1;
  117. } else {
  118. return expectedSize < 1073741824 ? (int)((float)expectedSize / 0.75F + 1.0F) : 2147483647;
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement