-1

I have class A and class B. class A sets various flags(boolean variable) of class B during the text mining. And in final step, class B takes decision for different combinations of these flags. There are approx 10 boolean variables (may increase in future).

I wonder if I use single byte comparison instead of multiple boolean comparisons. So I'll not have to maintain big list of boolean variables as well as multiple comparisons can be avoided.

I have attempted it using bitwise operators while setting the values or in comparison Eg :

if(byteFlag == xb001010)

if((byteFlag ^ xb0000011) == xb0000011)

*plz ignore the raw code given above, this is just a rough example.

But I ended up with bad code which even dint gave me any performance improvement.

Please advice me if someone has used byte comparison instead of multiple comparison before or how to do this for better performance.

Amit Kumar Gupta
  • 7,193
  • 12
  • 64
  • 90
  • 2
    This class may be of some help: http://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html – Fildor Aug 24 '15 at 06:59
  • but why don't you use `array` or `vector` of `boolean`? – Yazan Aug 24 '15 at 07:09
  • you can use the bitwise OR operator to 'set' your flags – strider Aug 24 '15 at 07:11
  • If you're having to care about such low level performance tweaks (possibly at the cost of readability) then maybe you should consider a lower level language to solve this problem. – StuPointerException Aug 24 '15 at 07:15
  • There is likely to be a performance improvement but it may be too small to measure. If your program takes 5 ns longer but is less readable, is it worth it? – Peter Lawrey Aug 24 '15 at 07:15
  • It'll be worthy if it is at least more than 5 ns ;) . ok so my program is about text mining. And I am gonna mine a complete book. So these smaller things may matter. – Amit Kumar Gupta Aug 24 '15 at 10:24
  • @StuPointerException, I am just refraining myself to learn any other language. In addition, Its like a big project, transforming it completely in other language will be more tuff for me. And I can mesh up everything with bad design, bad code, performance etc. – Amit Kumar Gupta Aug 25 '15 at 05:26

3 Answers3

0

I approve of the BitSet option mentioned in the comments, it will probably make your code more legible. If you want to experiment with bitwise operations and comparing your flags in bulk you can use | (bitwise or) to set the flags. You can compare them as int (or even long in 64bit processing):

public static final int FLAG_1 = 1;
public static final int FLAG_2 = 1 << 1;
public static final int FLAG_3 = 1 << 2;
//etc..
private int flags = 0;

public void setFlag(int flag){
    flags = flags | flag; // use bitwise 'or'
}
public boolean compareFlags(int otherFlags){
    return flags == otherFlags;
}
ljgw
  • 2,751
  • 1
  • 20
  • 39
  • Another reason to use `BitSet` would be for the arbitrary bit length. You'll eventually run out of bits for flags with the `int` – strider Aug 24 '15 at 07:18
0

Consider using an enum to indicate each of the properties that the text can have, and store the set of properties present in an EnumSet. The EnumSet does the kind of bit twiddling you suggest for you.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
0

You can do two things:

-Reduce your expressions with Bool's Algebra

-Use binary mask

For example

    int b1=(true)? 0: 1; //Set your real condition instead of true
    int b2=(true)? 0: 2;
    int b3=(true)? 0: 4;
    int b4=(true)? 0: 8;



    // for example if you want to have b1 and b2 true and to test it
    int argument=7;
    boolean example = (argument |b1+b2) ==b1+b2;
Michiel
  • 4,160
  • 3
  • 30
  • 42
Erwan C.
  • 709
  • 5
  • 18