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   45.9ms   46.6ms      21.2   16.29MB     9.08
#> 2 slope_terra    44.8ms   46.3ms      21.6    1.96MB    10.8

That is approximately

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

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    46.4ms     48ms      20.9    5.28MB    26.2 
#> 2 bilinear2    46.3ms   46.9ms      20.3    1.86MB     5.07
#> 3 simple1      37.8ms     38ms      26.0    1.97MB     7.80
#> 4 simple2      39.6ms   40.1ms      24.8    1.98MB     8.28
round(res$`itr/sec` * nrow(r))
#> [1] 5670 5491 7042 6732