3
3
import java .util .BitSet ;
4
4
5
5
public class BitSetTest {
6
- private static final int RUNS = 100000 ;
6
+ private static final int RUNS = 1000000 ;
7
7
private static final int NUM_BITS = 64 ;
8
- // every other bit set
9
- private static long setMask = 0xAAAAAAAAAAAAAAAAl ;
8
+ private static long data = 0b0011100001101100001000100000010010101010001110000011000000000100l;
10
9
11
10
private static long nativeBitTest () {
12
- long bits = 0 ;
13
11
int found = 0 ;
14
- bits |= setMask ;
15
12
16
13
long start = System .currentTimeMillis ();
17
14
18
15
for (int bit = 0 ; bit < NUM_BITS ; ++bit ) {
19
- if (((bits >>> bit ) & 0b1) == 0 ) {
16
+ if (((data >> bit ) & 0b1) == 1 ) {
20
17
++found ;
21
18
}
22
19
}
23
20
24
- System .out .printf ("found: %s\n " , found );
21
+ long time = System .currentTimeMillis () - start ;
22
+
23
+ //System.out.printf("found: %s\n", found);
25
24
26
- return System . currentTimeMillis () - start ;
25
+ return time ;
27
26
}
28
27
29
- private static long bitsetBitTest () {
28
+ private static long bitsetBitTest () {
29
+ // setup
30
30
BitSet bits = new BitSet (NUM_BITS );
31
- for (int bit = 1 ; bit < NUM_BITS ; bit += 2 ) {
32
- bits .set (bit );
31
+ for (int bit = 0 ; bit < NUM_BITS ; ++bit ) {
32
+ if (((data >> bit ) & 0b1) == 0 ) {
33
+ bits .set (bit );
34
+ }
33
35
}
34
36
35
37
long start = System .currentTimeMillis ();
@@ -39,9 +41,11 @@ private static long bitsetBitTest() {
39
41
++found ;
40
42
}
41
43
42
- System .out .printf ("found: %s\n " , found );
44
+ long time = System .currentTimeMillis () - start ;
45
+
46
+ //System.out.printf("found: %s\n", found);
43
47
44
- return System . currentTimeMillis () - start ;
48
+ return time ;
45
49
}
46
50
47
51
public static void main (String [] args ) {
0 commit comments