paleolimbot / geos

Open Source Geometry Engine ('GEOS') R API
https://paleolimbot.github.io/geos/
Other
61 stars 8 forks source link

Reduce overhead of R function calls #48

Closed paleolimbot closed 3 years ago

paleolimbot commented 3 years ago

Two improvements:

library(geos)
g <- geos_geometry()

san_geom <- function(x) {
  if (inherits(x, "geos_geometry")) x else as_geos_geometry(x)
}

bench::mark(geos_geometry(g))
#> # A tibble: 1 x 6
#>   expression            min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>       <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 geos_geometry(g)   6.17µs   10.2µs    69568.        0B     20.9
bench::mark(as_geos_geometry(g), san_geom(g))
#> # A tibble: 2 x 6
#>   expression               min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>          <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 as_geos_geometry(g)   1.37µs   1.61µs   557033.    3.51KB        0
#> 2 san_geom(g)            496ns    666ns   814815.        0B        0

Created on 2021-05-02 by the reprex package (v0.3.0)

Created on 2021-05-02 by the reprex package (v0.3.0)