Skip to contents
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   42.8ms     43ms      23.0   19.39MB     9.21
#> 2 slope_terra    40.5ms     41ms      24.3    1.97MB     9.13

That is approximately

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

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    42.9ms   43.4ms      23.0    5.35MB    13.1 
#> 2 bilinear2    41.6ms   42.6ms      23.5     1.9MB     7.83
#> 3 simple1      35.5ms   36.7ms      26.7    2.01MB    11.9 
#> 4 simple2      36.2ms   36.5ms      27.2    2.02MB     8.17
round(res$`itr/sec` * nrow(r))
#> [1] 6233 6365 7244 7380