library(ggplot2) library(reshape)
sum1 <- function(m) { res = 0 for(i in 1:dim(m)[1]) { for(j in 1:dim(m)[2]) { res <- res + m[i,j] } } return(res) }
sum2 <- function(m) { res = .C("sum2" ,mtrx=as.double(m) ,m=as.integer(dim(m)[1]) ,n=as.integer(dim(m)[2]) ,res=as.double(0) ) return(res$res) }
dyn.load("sum2.so")
msizes = c() times_1 = c() times_2 = c() for(i in (1:100) * 10) { m <- matrix(rnorm(i^2), i) times_1 = append(times_1, summary(system.time(sum1(m)))[1]) times_2 = append(times_2, summary(system.time(sum2(m)))[1]) msizes = append(msizes, i) } df = data.frame(size=msizes, sum_R = times_1, sum_c = times_2)
ggplot(data=melt(df, id.vars="size"), aes(x=size, y=value)) + geom_point(aes(shape=variable)) + geom_smooth(aes(color=variable))
`geom_smooth()` using method = 'loess' and formula 'y ~ x'