Further optimization taking advantage of correlation pre-computation
Further optimization taking advantage of correlation pre-computation

Rather than compute the correlation on every iteration, it is possible
to compute most of it just once outside the Monte Carlo loop (e.g. the
X residuals, sum-of-squares, etc.). In addition, the Y residuals will
be the same set of n! numbers on every iteration, just in a different
permutation. This means you can simply permute the Y residuals on each
iteration. These optimizations make the Mantel test about 4 times
faster (for Pearson) and about 20 times faster (for Spearman) !!!

jwcarr/MantelTest