Benchmarking slopes calculation

library(slopes)
library(bench)
library(raster)
#> Loading required package: sp

Performance

A benchmark can reveal how many route gradients can be calculated per second:

e = dem_lisbon_raster
r = lisbon_road_network
et = terra::rast(e)
res = bench::mark(check = FALSE,
  slope_raster = slope_raster(r, e),
  slope_terra = slope_raster(r, et)
)
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 slope_raster   44.2ms   45.1ms      21.3   19.61MB     4.73
#> 2 slope_terra    43.8ms   44.2ms      22.5    1.94MB     5.64

That is approximately

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

routes per second using the raster and terra (the default if installed, using RasterLayer and native SpatRaster objects) packages to extract elevation estimates from the raster datasets, respectively.

The message: use the terra package to read-in DEM data for slope extraction if speed is important.

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

e = dem_lisbon_raster
r = lisbon_road_network
res = bench::mark(check = FALSE,
  bilinear1 = slope_raster(r, e),
  bilinear2 = slope_raster(r, et),
  simple1 = slope_raster(r, e, method = "simple"),
  simple2 = slope_raster(r, et, method = "simple")
)
res
#> # A tibble: 4 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 bilinear1    44.8ms   45.6ms      21.7    5.28MB     8.14
#> 2 bilinear2    43.4ms   44.5ms      22.4    1.86MB     4.97
#> 3 simple1      36.9ms   37.5ms      26.6    1.97MB     4.83
#> 4 simple2      37.8ms   39.3ms      25.5    1.98MB     4.64
round(res$`itr/sec` * nrow(r))
#> [1] 5885 6065 7206 6910