Skip to content

Instantly share code, notes, and snippets.

@tylermorganwall
Created November 11, 2022 06:47
Show Gist options
  • Save tylermorganwall/0a67ceabc8d9f1efd5f5008cabd47a93 to your computer and use it in GitHub Desktop.
Save tylermorganwall/0a67ceabc8d9f1efd5f5008cabd47a93 to your computer and use it in GitHub Desktop.
vapply
testapply = list()
testvapply = list()
# First, we generate a 10001 x 10001 matrix of random numbers pulled from a gaussian distribution. Our goal is to sum each of the columns and return a vector of each sum. Here, we have two strategies:
# 1) Loop through each column, calculate the sum, and assign into a vector.
# 2) apply() the sum function to each column.
numbers = matrix(rnorm(10001^2,0,1),nrow=10001,ncol=10001)
iter=1
for(max in seq(1,601,25)) {
print(max)
nnumbers = numbers[,1:max,drop=FALSE]
# First: the for loop.
initialtime = proc.time()[3]
totalsum = rep(0,max)
for(i in 1:max) {
totalsum[i] = sum(nnumbers[,i,drop=FALSE])
}
testapply$timeloop[iter] = proc.time()[3] - initialtime
# Now timing the apply function. The 2 in apply() tells R to sum the matrix nnumbers by column.
initialtime = proc.time()[3]
totalsum = apply(nnumbers,2,sum)
testapply$timeapply[iter] = proc.time()[3] - initialtime
testapply$iteration[iter] = max
initialtime = proc.time()[3]
totalsum = vapply(nnumbers,sum,FUN.VALUE = numeric(1))
testvapply$timeapply[iter] = proc.time()[3] - initialtime
testvapply$iteration[iter] = max
iter = iter + 1
}
forvsapplydf = as.data.frame(do.call(cbind,testapply) )
forvsvapplydf = as.data.frame(do.call(cbind,testvapply) )
Expand Plotting Code
ggplot() +
geom_line(data=forvsapplydf,aes(x=iteration,y=timeloop),color="green") +
geom_line(data=forvsapplydf,aes(x=iteration,y=timeapply),color="red") +
geom_line(data=forvsvapplydf,aes(x=iteration,y=timeapply),color="blue") +
theme(text=element_text(family="Helvetica"),
panel.grid.major.y = element_line(color = "grey90"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment