Question: I'm trying to compare 2 large vectors(integer) i.e. at each entry, see if two vectors have the same element or not. I've tried a few things, using an iterator to do the comparision and a simple for loop. Both works but I need something that will speed things up as I have to compare a lot of vectors. What's the best way to do that in C++?? Many thanks in advance!

```
typedef vector<int> fingerprint;

double aakernel(fingerprint a,fingerprint b, double h){

    double diff = 0;
    vector<int>::iterator dd = a.begin();
    vector<int>::iterator ee = b.begin();

    for(; dd != a.end() && ee != b.end() ;++dd, ++ee){ /*option one*/
        if (*dd!=*ee){
            diff++;
        }

    }

    for (int dd=0;dd<int(a.size());dd++){ /*option two*/
        if (a[dd]!=b[dd]){
            diff++;
        }
    }
    double due = (h/(1-h));
    double q = -log(due)*diff;
    double K = exp(q);
    return (K);
}

```

Answer: 
If the vectors are otherwise arbitrary, you cannot get asymptotically better than sequentially comparing all elements, the way you do now. So you're left with micro-optimisations which may or may not improve performance (depending on how your compiler's optimiser handles them).

The only one I can think of is taking the non-changing evaluations out of the loop. (And perhaps also not using `++` on type `double`, but I believe the compiler will handle this optimally anyway):

```
double diff = 0;
for (
  auto itA = a.begin(), itB = b.begin(), endA = a.end();
  itA != endA;
  ++itA, ++itB
) {
  if (*itA != *itB) {
    diff += 1.0;
  }
}

```
In case the two `fingerprint` values are usually the same, it may help if you first do a

```
memcmp(&a[0], &b[0], a.size() * sizeof(int))

```

To test whether there's any difference between the two arrays at all. Only if there's any difference you go and look how many differences there are.