Note: benchmarks do not currently evaluate on actions for faster build times.

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)
)
#> Warning: Some expressions had a GC in every iteration; so filtering is disabled.
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    104ms    111ms      8.46   22.49MB     3.38
#> 2 slope_terra     139ms    277ms      3.61    2.17MB     3.61

That is approximately

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

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   127.6ms    128ms      7.84    5.72MB     7.84
#> 2 bilinear2     118ms    144ms      7.17    2.12MB     2.39
#> 3 simple1      90.9ms     93ms      8.82    2.14MB     4.41
#> 4 simple2     112.3ms    119ms      8.34    2.12MB     5.56