Skip to content

Instantly share code, notes, and snippets.

@lemire
Last active March 15, 2019 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemire/9e83b22d19b19a687ff18573311538da to your computer and use it in GitHub Desktop.
Save lemire/9e83b22d19b19a687ff18573311538da to your computer and use it in GitHub Desktop.
import org.roaringbitmap.RoaringBitmap;
import org.roaringbitmap.buffer.*;
import java.io.*;
import java.nio.*;
public class Issue319 {
public static void main(String[] args) throws IOException {
RoaringBitmap mrb = RoaringBitmap.bitmapOf(1,2,3,1000);
for(int k = 0; k < 1000000000; k+=13) mrb.add(k);
mrb.runOptimize();
int count = 30;
byte[] array = new byte[mrb.serializedSizeInBytes()];
try {
mrb.serialize(new java.io.DataOutputStream(new java.io.OutputStream() {
int c = 0;
@Override
public void close() {
}
@Override
public void flush() {
}
@Override
public void write(int b) {
array[c++] = (byte)b;
}
@Override
public void write(byte[] b) {
write(b,0,b.length);
}
@Override
public void write(byte[] b, int off, int l) {
System.arraycopy(b, off, array, c, l);
c += l;
}
}));
} catch (IOException ioe) {
// should never happen because we write to a byte array
throw new RuntimeException("unexpected error while serializing to a byte array");
}
long bef, aft;
long sum = 0;
System.out.println("recommended: ");
for(int k = 0; k < count; k++) {
bef = System.currentTimeMillis();
RoaringBitmap ret = new RoaringBitmap();
try {
ret.deserialize(new java.io.DataInputStream(new java.io.InputStream() {
int c = 0;
@Override
public int read() {
return array[c++] & 0xff;
}
@Override
public int read(byte b[]) {
return read(b, 0, b.length);
}
@Override
public int read(byte[] b, int off, int l) {
System.arraycopy(array, c, b, off, l);
c += l;
return l;
}
}));
} catch (IOException ioe) {
// should never happen because we read from a byte array
throw new RuntimeException("unexpected error while deserializing from a byte array");
}
aft = System.currentTimeMillis();
System.out.print(aft-bef+" ms ");
sum += aft - bef;
if(!ret.equals(mrb)) throw new RuntimeException("bug");
}
System.out.println("\naverage: "+sum/count);
System.out.println("via ByteArrayInputStream: ");
sum = 0;
for(int k = 0; k < count; k++) {
bef = System.currentTimeMillis();
RoaringBitmap ret = new RoaringBitmap();
ret.deserialize(new DataInputStream(new ByteArrayInputStream(array)));
aft = System.currentTimeMillis();
System.out.print(aft-bef+" ms ");
sum += aft - bef;
if(!ret.equals(mrb)) throw new RuntimeException("bug");
}
System.out.println("\naverage: "+sum/count);
System.out.println("via Immutable: ");
sum = 0;
for(int k = 0; k < count; k++) {
bef = System.currentTimeMillis();
RoaringBitmap ret = new ImmutableRoaringBitmap(ByteBuffer.wrap(array)).toRoaringBitmap();
aft = System.currentTimeMillis();
System.out.print(aft-bef+" ms ");
sum += aft - bef;
if(!ret.equals(mrb)) throw new RuntimeException("bug");
}
System.out.println("\naverage: "+sum/count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment