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     47.6ms   49.1ms      20.5   17.86MB     16.4
#> 2 simple       41.4ms     42ms      23.8    1.88MB     13.6

That is approximately

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

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     48.2ms   50.8ms      19.8    1.73MB     8.50
#> 2 simple         42ms     42ms      23.5    1.81MB    13.4
round(res2$`itr/sec` * nrow(r))
#> [1] 5373 6368