rgdal-dev / rasterwise

Hard-won lessons! Don't lose 'em!
4 stars 0 forks source link

benchmark zoo #8

Open mdsumner opened 5 years ago

mdsumner commented 5 years ago

Soon we need benchmarks of reprojecting, polygonizing, rasterizing and so forth

Here's rasterizing:

library(raster)
library(fasterize)
library(sf)
p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20))
p1 <- list(p1, hole)
p2 <- list(rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0)))
p3 <- list(rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0)))
pols <- st_sf(value = c(1,2,3),
              geometry = st_sfc(lapply(list(p1, p2, p3), st_polygon)))
r0 <- raster(pols, res = 1)
pols_r <- as(pols, "Spatial")
stars_grid <- stars::st_as_stars(raster(r0))

library(terra)
Sr <- rast(r0)
Sp <- vect(pols_r)
bench <- microbenchmark::microbenchmark(
  rasterize = r <- raster::rasterize(pols_r, r0, field = pols$value),
  fasterize = f <- fasterize(pols, r0, field = "value"),
  starseyes = s <- stars::st_rasterize(pols, stars_grid),
  terraize = t <- terra::rasterize(Sp, Sr), 
  times = 20,
  unit = "ms"
)
print(bench, digits = 3)

data("wrld_simpl", package = "maptools")
w <- sf::st_as_sf(wrld_simpl)
w$index <- 1:nrow(w)
r0 <- raster(w)
res(r0) <- 0.1
stars_grid <- stars::st_as_stars(raster(r0))
Sr <- rast(r0)
Sp <- vect(wrld_simpl)
bench <- microbenchmark::microbenchmark(
  #rasterize = r <- raster::rasterize(w, r0, field = "index"),
  fasterize = f <- fasterize(w, r0, field = "index"),
  #starseyes = s <- stars::st_rasterize(w, stars_grid),
  terraize = t <- terra::rasterize(Sp, Sr), 
  times = 1,
  unit = "ms"
)
print(bench, digits = 3)
Unit: milliseconds
      expr    min     lq    mean  median      uq    max neval cld
 rasterize 11.053 11.967  15.742  14.262  18.250  27.25    20  b 
 fasterize  0.322  0.389   0.621   0.565   0.687   1.58    20 a  
 starseyes 81.404 97.938 107.542 100.954 106.003 185.99    20   c
  terraize  3.037  3.817   4.378   4.303   4.778   5.92    20 a