Skip to content

Instantly share code, notes, and snippets.

@wviechtb
Last active May 4, 2022 08:25
Show Gist options
  • Save wviechtb/c224c7abb8f17277b9e768abb52847c8 to your computer and use it in GitHub Desktop.
Save wviechtb/c224c7abb8f17277b9e768abb52847c8 to your computer and use it in GitHub Desktop.
Comparison of apply() versus for-loop in 3 versions of R
############################################################################
R version 2.5.0 (2007-04-23)
Copyright (C) 2007 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> n <- 2000
> x <- matrix(runif(n*n), nrow=n, ncol=n)
>
> f1 <- function(x) {
+ nr <- nrow(x)
+ m <- rep(NA_real_, nr)
+ for (i in 1:nr) {
+ m[i] <- mean(x[i,])
+ }
+ return(m)
+ }
> f2 <- function(x) apply(x, 1, mean)
> f3 <- function(x) rowMeans(x)
>
> system.time(replicate(100, f1(x)))
user system elapsed
5.659 0.015 5.677
> system.time(replicate(100, f2(x)))
user system elapsed
8.363 0.016 8.380
> system.time(replicate(100, f3(x)))
user system elapsed
0.725 0.000 0.725
############################################################################
R version 3.0.0 (2013-04-03) -- "Masked Marvel"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-unknown-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> n <- 2000
> x <- matrix(runif(n*n), nrow=n, ncol=n)
>
> f1 <- function(x) {
+ nr <- nrow(x)
+ m <- rep(NA_real_, nr)
+ for (i in 1:nr) {
+ m[i] <- mean(x[i,])
+ }
+ return(m)
+ }
> f2 <- function(x) apply(x, 1, mean)
> f3 <- function(x) rowMeans(x)
>
> system.time(replicate(100, f1(x)))
user system elapsed
5.069 0.000 5.070
> system.time(replicate(100, f2(x)))
user system elapsed
7.561 0.008 7.570
> system.time(replicate(100, f3(x)))
user system elapsed
0.724 0.000 0.725
############################################################################
R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> n <- 2000
> x <- matrix(runif(n*n), nrow=n, ncol=n)
>
> f1 <- function(x) {
+ nr <- nrow(x)
+ m <- rep(NA_real_, nr)
+ for (i in 1:nr) {
+ m[i] <- mean(x[i,])
+ }
+ return(m)
+ }
> f2 <- function(x) apply(x, 1, mean)
> f3 <- function(x) rowMeans(x)
>
> system.time(replicate(100, f1(x)))
user system elapsed
4.915 0.020 4.936
> system.time(replicate(100, f2(x)))
user system elapsed
5.072 0.036 5.108
> system.time(replicate(100, f3(x)))
user system elapsed
0.635 0.000 0.635
############################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment