The following simply for block takes about ~3 sec to complete in R: library(MASS) nruns <- 2000 nelems <- 50 maxX <- 1 maxY <- 1 for(i in 1:nruns) { dataX <- runif(nelems, 0, maxX) dataY <- runif(nelems, 0, maxY) kde2d(dataX, dataY, n=50, lims=c(0, maxX, 0, maxY) ) } The same code run in Python through the rpy2 library takes between 4-5 times more: from rpy2.robjects import r from rpy2.robjects.packages import importr importr('MASS') nruns = 2000 r.assign('nelems', 50) r.assign('maxX', 1) r.assign('maxY', 1) for _ in range(nruns): r('dataX <- runif(nelems, 0, maxX)') r('dataY <- runif(nelems, 0, maxY)') r('kde2dmap <- kde2d(dataX, dataY, n=50, lims=c(0, maxX, 0, maxY))') Is this just because I'm using the rpy2 library to communicate with R or is there something else at play? Can this be improved in any way (while still running the code in Python)? Continue reading...