Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. import org.roaringbitmap.RoaringBitmap;
  2. import org.roaringbitmap.buffer.*;
  3.  
  4. import java.io.*;
  5. import java.nio.*;
  6.  
  7. public class Issue319 {
  8.  
  9.  
  10.  
  11. public static void main(String[] args) throws IOException {
  12. RoaringBitmap mrb = RoaringBitmap.bitmapOf(1,2,3,1000);
  13. for(int k = 0; k < 1000000000; k+=13) mrb.add(k);
  14. mrb.runOptimize();
  15. int count = 30;
  16. byte[] array = new byte[mrb.serializedSizeInBytes()];
  17. try {
  18. mrb.serialize(new java.io.DataOutputStream(new java.io.OutputStream() {
  19. int c = 0;
  20.  
  21. @Override
  22. public void close() {
  23. }
  24.  
  25. @Override
  26. public void flush() {
  27. }
  28.  
  29. @Override
  30. public void write(int b) {
  31. array[c++] = (byte)b;
  32. }
  33.  
  34. @Override
  35. public void write(byte[] b) {
  36. write(b,0,b.length);
  37. }
  38.  
  39. @Override
  40. public void write(byte[] b, int off, int l) {
  41. System.arraycopy(b, off, array, c, l);
  42. c += l;
  43. }
  44. }));
  45. } catch (IOException ioe) {
  46. // should never happen because we write to a byte array
  47. throw new RuntimeException("unexpected error while serializing to a byte array");
  48. }
  49. long bef, aft;
  50. long sum = 0;
  51. System.out.println("recommended: ");
  52.  
  53. for(int k = 0; k < count; k++) {
  54. bef = System.currentTimeMillis();
  55. RoaringBitmap ret = new RoaringBitmap();
  56. try {
  57. ret.deserialize(new java.io.DataInputStream(new java.io.InputStream() {
  58. int c = 0;
  59.  
  60. @Override
  61. public int read() {
  62. return array[c++] & 0xff;
  63. }
  64.  
  65. @Override
  66. public int read(byte b[]) {
  67. return read(b, 0, b.length);
  68. }
  69.  
  70. @Override
  71. public int read(byte[] b, int off, int l) {
  72. System.arraycopy(array, c, b, off, l);
  73. c += l;
  74. return l;
  75. }
  76. }));
  77. } catch (IOException ioe) {
  78. // should never happen because we read from a byte array
  79. throw new RuntimeException("unexpected error while deserializing from a byte array");
  80. }
  81. aft = System.currentTimeMillis();
  82. System.out.print(aft-bef+" ms ");
  83. sum += aft - bef;
  84. if(!ret.equals(mrb)) throw new RuntimeException("bug");
  85. }
  86. System.out.println("\naverage: "+sum/count);
  87.  
  88.  
  89. System.out.println("via ByteArrayInputStream: ");
  90. sum = 0;
  91. for(int k = 0; k < count; k++) {
  92. bef = System.currentTimeMillis();
  93. RoaringBitmap ret = new RoaringBitmap();
  94. ret.deserialize(new DataInputStream(new ByteArrayInputStream(array)));
  95. aft = System.currentTimeMillis();
  96. System.out.print(aft-bef+" ms ");
  97. sum += aft - bef;
  98. if(!ret.equals(mrb)) throw new RuntimeException("bug");
  99. }
  100. System.out.println("\naverage: "+sum/count);
  101.  
  102. System.out.println("via Immutable: ");
  103. sum = 0;
  104. for(int k = 0; k < count; k++) {
  105. bef = System.currentTimeMillis();
  106. RoaringBitmap ret = new ImmutableRoaringBitmap(ByteBuffer.wrap(array)).toRoaringBitmap();
  107. aft = System.currentTimeMillis();
  108. System.out.print(aft-bef+" ms ");
  109. sum += aft - bef;
  110. if(!ret.equals(mrb)) throw new RuntimeException("bug");
  111. }
  112. System.out.println("\naverage: "+sum/count);
  113.  
  114.  
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement