KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
OK, we're all familiar with double colon operator in R. Whenever I'm about to write some function, I use require(<pkgname>) , but I was always thinking about using :: instead. Using require in custom functions is better practice than library , since require returns warning and FALSE , unlike library , which returns error if you provide a name of non-existent package. On the other hand, :: operator gets the variable from the package, while require loads whole package (at least I hope so), so speed differences came first to my mind. :: must be faster than require . And I did some analysis in order to check that - I've written two simple functions that load read.systat function from foreign package, with require and :: respectively, hence import Iris.syd dataset that ships with foreign package, replicated functions 1000 times each (which was shamelessly arbitrary), and... crunched some numbers. Strangely (or not) I found significant differences in terms of user CPU and elapsed time, while there were no significant differences in terms of system CPU. And yet more strange conclusion: :: is actually slower! Documentation for :: is very blunt, and just by looking at sources it's obvious that :: should perform better! require #!/usr/local/bin/r ## with require fn1 <- function() { require(foreign) read.systat("Iris.syd", to.data.frame=TRUE) } ## times n <- 1e3 sink("require.txt") print(t(replicate(n, system.time(fn1())))) sink() double colon #!/usr/local/bin/r ## with :: fn2 <- function() { foreign::read.systat("Iris.syd", to.data.frame=TRUE) } ## times n <- 1e3 sink("double_co
Tags (comma-separated)
Save Edits
Cancel