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     32.4ms   34.6ms      29.0   17.71MB     20.7
#> 2 simple       28.7ms   29.9ms      33.0    1.88MB     16.5

That is approximately

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

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     33.3ms   36.8ms      27.1    1.73MB     17.0
#> 2 simple       29.5ms   33.1ms      31.0    1.81MB     11.3
round(res2$`itr/sec` * nrow(r))
#> [1] 7353 8391