Benchmarking slopes calculation

library(slopes)
library(bench)

Performance

A benchmark can reveal how many route gradients can be calculated per second using different interpolation methods:

e = dem_lisbon()
r = lisbon_road_network
res = bench::mark(check = FALSE,
  bilinear = slope_raster(r, e),
  simple   = slope_raster(r, e, method = "simple")
)
res
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 bilinear       20ms   20.3ms      48.7   17.98MB     39.8
#> 2 simple       17.3ms   17.7ms      56.3    2.01MB     41.3

That is approximately

round(res$`itr/sec` * nrow(r))
#> [1] 13194 15249

routes per second using bilinear and simple interpolation methods, respectively.

To go faster, you can chose the simple method to gain some speed at the expense of accuracy:

res2 = bench::mark(check = FALSE,
  bilinear = slope_raster(r, e, method = "bilinear"),
  simple   = slope_raster(r, e, method = "simple")
)
res2
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 bilinear       21ms   21.7ms      45.6    1.86MB     41.5
#> 2 simple       17.3ms   18.1ms      54.4    1.94MB     49.4
round(res2$`itr/sec` * nrow(r))
#> [1] 12369 14735