14

I have two Javascript ArrayBuffers; each contains 512 bits of data. I would like to do an xor comparison of the two arrays and store the results in a third array.

Currently, I am looping over the elements in the buffers. In the code below 'distance' is an integer and feat_a1 and feat_b1 are ArrayBuffers that are 512 bits in length.

for(var d1=0; d1<512; d1++){
  distance += feat_b1[d1] ^ feat_a1[d1];
}

Is there a more efficient way of doing the pairwise comparison of these two arrays?

DwB
  • 37,124
  • 11
  • 56
  • 82
freakTheMighty
  • 1,172
  • 1
  • 12
  • 27
  • 1
    I don't believe so. Depending on your environment, you may squeeze a little more out of it by using `while` instead, you would need to test. Even unrolling the loop can make it slower on recent browsers because of their optimisations. – Xotic750 Feb 03 '14 at 16:57
  • Does distance (which I'm assuming to be your third array) contain data already? If not, depending on implementation, you could get a tiny bit (as in, fractions of fractions of milliseconds even on large data sets) more from using a simple '=' since technically it's one less memory reference per operation. – Matthew Herbst Feb 03 '14 at 17:53
  • What exactly do you need this for? I had expected `distance` to be another `ArrayBuffer` when you said you wanted to XOR them… Also, is it 512 bits (how do you access them individually?) or is it 512 bytes? It would be nice if you could also include the code that initializes your `feat_b1` and `feat_a1`. – Bergi Feb 03 '14 at 18:37
  • Right now 'distance' is an integer. I am summing the xored bits as I go. I was hoping that Javascript had some sort of built in and optimized way of doing pairwise comparisons of ArrayBuffers. Something like buff1 ^ buff2 -> new_buff. With that done, the next step would have been to sum the bits in new_buff. – freakTheMighty Feb 03 '14 at 19:51
  • Thanks to DwB for the edits. – freakTheMighty Feb 03 '14 at 20:10
  • Again: [What are you doing?](http://meta.stackexchange.com/q/66377) What does `distance` represent, is it some bit-wise edit distance? What do you mean by "sum the bits"? – Bergi Feb 03 '14 at 23:44
  • @Bergi I think it is clearly that OP want [hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) of two 512 bits ArrayBuffer. – tsh Jan 22 '18 at 08:58
  • @tsh Only that array buffers are measured in bytes not bits, and XOR works on uint32 in JS. Yes, it appears to be a hamming distance, but the OP had not used that term anywhere. – Bergi Jan 22 '18 at 09:13

1 Answers1

1

as i understand you can't directly use arrayBuffer[i], you have to pass it to some container (like Int8Array). I have made next example http://jsfiddle.net/mLurz/ , tried with different Typed arrays from this list and Uint32Array showed the best performance

var i;
var dist = 0;
var max = Math.pow(2,32);
var buf1 = new ArrayBuffer(1024);
var x = new Uint32Array(buf1);
for (i = 0; i < 256; ++i) {
    x[i] = Math.random()*max;
}

var buf2 = new ArrayBuffer(1024);
var y = new Uint32Array(buf2);
for (i = 0; i < 256; ++i) {
    y[i] = Math.random()*max
}

console.time('Uint32Array');
for (var j = 0; j < 1000000; ++j) {
    for (i = 0; i < 256; ++i) {
        dist += y[i]^x[i];
    }
}
console.timeEnd('Uint32Array');
Vlad Nikitin
  • 1,929
  • 15
  • 17
  • Sorry, I think I am already using a typed array. I think I was in accurate with my language. I suppose the typed array provides a mask for an ArrayBuffer? – freakTheMighty Feb 03 '14 at 20:03