What is the difference between require() and library()?

前端 未结 8 1188
情话喂你
情话喂你 2020-11-22 11:00

What is the difference between require() and library()?

8条回答
  •  执念已碎
    2020-11-22 11:43

    Here seems to be the difference on an already loaded package. While it is true that both require and library do not load the package. Library does a lot of other things before it checks and exits.

    I would recommend removing "require" from the beginning of a function running 2mil times anyway, but if, for some reason I needed to keep it. require is technically a faster check.

    microbenchmark(req = require(microbenchmark), lib = library(microbenchmark),times = 100000)
    Unit: microseconds
     expr    min     lq      mean median     uq        max neval
      req  3.676  5.181  6.596968  5.655  6.177   9456.006 1e+05
      lib 17.192 19.887 27.302907 20.852 22.490 255665.881 1e+05
    

提交回复
热议问题