Last active
December 3, 2019 07:05
-
-
Save jonocarroll/e4b191357dbdb853fff99616bc9c3a23 to your computer and use it in GitHub Desktop.
R vs Julia sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fastsort <- function(x, partial) { | |
y <- if (length(partial) <= 10L) { | |
partial <- .Internal(qsort(partial, FALSE)) | |
.Internal(psort(x, partial)) | |
} | |
else { | |
.Internal(qsort(x, FALSE)) | |
} | |
y | |
} | |
sim2 <- function(n, k, m, iters) { | |
f <- function(i) { | |
x1 <- x2 <- runif(n) | |
x2[1:m] <- runif(m) | |
x.top1 <- fastsort(x1, partial=(n-k+1):n)[(n-k+1):n] | |
x.top2 <- fastsort(x2, partial=(n-k+1):n)[(n-k+1):n] | |
return(sum(match(x.top2, x.top1, nomatch = 0) > 0)) | |
} | |
v <- sapply(seq_len(iters), f) | |
sum(k*iters, -v)/iters | |
} | |
n <- 10 | |
k <- 4 | |
m <- 1 | |
set.seed(1) | |
system.time(print(sim(n, k, m, 1e6))) | |
#> [1] 0.617969 | |
#> user system elapsed | |
#> 34.990 0.156 35.239 | |
set.seed(1) | |
system.time(print(sim2(n, k, m, 1e6))) | |
#> [1] 0.617969 | |
#> user system elapsed | |
#> 10.892 0.073 11.010 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment