Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitSet.valueOf(), toByteArray() similar methods #1

Open
manikantag opened this issue May 18, 2015 · 3 comments
Open

BitSet.valueOf(), toByteArray() similar methods #1

manikantag opened this issue May 18, 2015 · 3 comments

Comments

@manikantag
Copy link

Hi,

Would be great if we've something like BitSet.valueOf() & 'BitSet.toByteArray()' in SparseBitSet too. Currently it is not direct to initialise SparseBitSet from byte[]

@manikantag manikantag changed the title BitSet.valueOf() similar method BitSet.valueOf(), toByteArray() similar methods May 18, 2015
@manikantag
Copy link
Author

valueOf can be implemented like below: (byte[] to bit extraction logic taken from http://stackoverflow.com/a/1034513/340290)

public static SparseBitSet valueOf(byte[] bytes) {
    SparseBitSet sparseBitSet = new SparseBitSet();
    int j = 0;
    for (byte b : bytes) {
        for (int mask = 0x01; mask != 0x100; mask <<= 1) {
            if((b & mask) != 0) {
                sparseBitSet.set(j);
            }
            j++;
        }
    }

    return sparseBitSet;
}

Similar method for toByteArray will make it easy to store the SparseBitSet as a hex or base64 string.

@manikantag
Copy link
Author

I did a simple benchmark to see which faster among BitSet & SparseBitSet to create from byte[].

SparseBitSet is taking ~10 times more time than BitSet.valueOf(). I m not sure if this is due to the above implementation of SparseBitSet.valueOf().

Can we improve this? Below is the very rudementary main() for testing:

public static void main(String[] args) throws DecoderException {
    int[] intArray = new int[] {0,1,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,0,1,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,0,1,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,0,1,0,1,1,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,0,1,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,0,1,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,0,1,0,1,1,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,0,1,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,0,1,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,0,1,0,1,1,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,0,1,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,0,1,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,0,1,0};

    BitSet bitSet = new BitSet();
    SparseBitSet sparseBitSet = new SparseBitSet();

    for(int i = 0; i < intArray.length; i++) {
        bitSet.set(i, intArray[i] == 1);
        sparseBitSet.set(i, intArray[i] == 1);
    }

    System.out.println("BitSet original      : " + bitSet);
    System.out.println("SparseBitSet original: " + sparseBitSet);

    String hexStr = Hex.encodeHexString(bitSet.toByteArray()); // BitSet --> Hex
    byte[] decodeHex = Hex.decodeHex(hexStr.toCharArray()); // Hex -> byte[]

    System.out.println("SparseBitSet decoded : " + sparseBitSetValueOf(decodeHex));

    // BitSet Hex
    long start1 = System.currentTimeMillis();
    for(int i = 0; i < 1000000; i++) {
        BitSet.valueOf(decodeHex);
    }
    long end1 = System.currentTimeMillis();
    System.out.println("BitSet Hex total time: " + (end1 - start1));

    // SparseBitSet Hex
    long start3 = System.currentTimeMillis();
    for(int i = 0; i < 1000000; i++) {
        sparseBitSetValueOf(decodeHex);
    }
    long end3 = System.currentTimeMillis();
    System.out.println("SparseBitSet Hex total time: " + (end3 - start3));

}

@lemire
Copy link

lemire commented May 18, 2015

@manikantag

SparseBitSet is serializable. Why don't you serialize and deserialize it instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants