---
title: "Benchmarking slopes calculation"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Benchmarking slopes calculation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(slopes)
library(bench)
library(raster)
```

# Performance

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

```{r, results='hide'}
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)
)
```

```{r}
res
```


That is approximately

```{r}
round(res$`itr/sec` * nrow(r))
```

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:

```{r, results='hide'}
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")
)
```

```{r}
res
```

```{r}
round(res$`itr/sec` * nrow(r))
```
