Vectorize or otherwise speed up a double for loop
Can't see exactly what's going with the sliding windows but it looks like you're calculating the full distance matrix: (i,j) and (j,i). If so, you probably only need half (usually lower) but you can make both with Distances[n1,n2] &lt;- Distances[n2,n1] &lt;- sum ... If this is correct, you'll basically double your speed.

But, if I misunderstood the above, you could still win a little time by reducing the number of index lookup, since these are really slow in R. In particular c(windowp1[n1]:windowp2[n1]) could be pulled out of the loop. Depending on what "x" looks like, you might try "xor(a, b)" rather than "a != b".

Adding a few other changes (eg, drop n1,n2 in favor of the loop variables) but leaving the possible "full matrix" thing I mentioned, here's a suggestion:

distances.ncol &lt;- ncol(Distances)
for(i in seq_along(windowp1)) {
    wp12 &lt;- c(windowp1[i]:windowp2[i])
    setTxtProgressBar(prog, i)

    for(j in seq_len(distances.ncol)) {
        Distances[i,j] &lt;-
            sum(x[pairs[j,1],wp12] != x[pairs[j,2],wp12])
    }
    Distances2 &lt;- (Distances/winsize)*100
}