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     45.2ms   45.6ms      21.9   17.85MB     17.5
#> 2 simple       37.9ms   38.8ms      25.7    1.88MB     12.9

That is approximately

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

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     46.3ms   47.3ms      21.1    1.73MB     14.1
#> 2 simple       38.8ms   39.6ms      25.3    1.81MB     12.7
round(res2$`itr/sec` * nrow(r))
#> [1] 5731 6861