I have a self-made class BitVector with a single field

private boolean[] bits; 

There is in the same class a small method that makes a conjunction (logical "and") for vectors:

 public BitVector and(BitVector anotherVector) { if (this == anotherVector) return (BitVector) clone(); if (size() != anotherVector.size()) throw new IllegalArgumentException("Incompatible sizes: size="+size()+", other.size()="+anotherVector.size()); BitVector intersectionVector = new BitVector(size()); for (int i = 0; i < size(); i++){ if (get(i) & anotherVector.get(i)) intersectionVector.set(i); } return intersectionVector; } public Object clone() { BitVector clone = null; try { clone = (BitVector) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } if (this.bits != null) clone.bits = this.bits.clone(); return clone; } public void set(int index){ bits[index] = true; } public boolean get(int index){ return bits[index]; } public int size() { return this.bits.length; } 

Is it possible to parallelize a single loop within this method using streams? Approximate sizes for the bits field - 1 million

  • If you are satisfied with the answer, mark it as correct (checkmark to the left of the answer). - andreycha

1 answer 1

Why not?

 IntStream.range(0, size()) .parallel() .forEach(i -> { if(get(i) & anotherVector.get(i)){ intersectionVector.set(i); } }); 

or so, but measurements showed that the first method is slightly faster

 IntStream.range(0, size()) .parallel() .filter(i -> get(i) & anotherVector.get(i)) .forEach(i -> intersectionVector.set(i) ); 
  • Thanks, I thought about this approach, I just don’t know if it will be thread-safe? - Stas0n
  • What is the case if another thread changes one of the vectors? Of course, I don’t know your task, but if your BitVector is immutable, then problems should not arise. For example, the set method can be encapsulated. - iksuy
  • ok, I'll try .. thanks - Stas0n
  • You can even use the parallel stream, it is faster, but not recommended. in my case it misses some elements - Senior Pomidor
  • I have a test case of 29 hours lasted. I divided it into 2 parts, I launch each part in different streams, and inside the stream I call parallelStream (). Case accelerated to 19 minutes. I don't notice any problems yet - Senior Pomidor