Skip to content

Instantly share code, notes, and snippets.

@symbolrush
Created March 31, 2016 10:25
Show Gist options
  • Save symbolrush/14716283f96d0c143e60d5a67f8f19c7 to your computer and use it in GitHub Desktop.
Save symbolrush/14716283f96d0c143e60d5a67f8f19c7 to your computer and use it in GitHub Desktop.
set.seed(1)
vec <- runif(1000)
vec1 <- vec
print(paste0("Die beiden Vektoren sind identisch: ", all.equal(vec, vec1)))
print(paste0("Die Variante for-loop dauert: ", summary(microbenchmark::microbenchmark(
for (i in 1:length(vec)) {
if (vec[i] > 0.5) {vec[i] <- 10}
}
), unit = "ms")$uq, "ms"))
print(paste0("Die Variante ifelse dauert: ", summary(microbenchmark::microbenchmark(
vec1 <- ifelse(vec1 > 0.5, 10, vec1)
), unit = "ms")$uq, "ms"))
print(paste0("Die beiden Vektoren sind auch nach der Operation identisch: ", all.equal(vec, vec1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment