Skip to contents
library(slopes)
library(bench)
#> Error in get(paste0(generic, ".", class), envir = get_method_env()) : 
#>   object 'type_sum.accel' not found
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   45.2ms   46.2ms      21.7   16.19MB     9.30
#> 2 slope_terra    41.9ms   43.9ms      23.0    1.96MB     9.84

That is approximately

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

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    45.1ms   45.4ms      22.0    5.28MB    22.0 
#> 2 bilinear2    40.8ms     42ms      23.7    1.86MB     7.89
#> 3 simple1      36.6ms     37ms      27.0    1.97MB     7.36
#> 4 simple2      36.5ms   37.6ms      26.6    1.98MB     7.98
round(res$`itr/sec` * nrow(r))
#> [1] 5950 6417 7317 7205