cyclestreets / cyclestreets-r

An R interface to cyclestreets.net APIs
https://rpackage.cyclestreets.net/
GNU General Public License v3.0
27 stars 7 forks source link

Speed up routing #46

Closed Robinlovelace closed 11 months ago

Robinlovelace commented 1 year ago

As raised by @mem48 here https://github.com/nptscot/cyclestreets-r/issues/1

Robinlovelace commented 1 year ago

Relevant PR: https://github.com/cyclestreets/cyclestreets-r/pull/45/files

Robinlovelace commented 1 year ago

Code in most recent commit linked to above shows that it is ~2x quicker.

> fromPlace = sf::st_as_sf(lwgeom::st_startpoint(od))
> toPlace = sf::st_as_sf(lwgeom::st_endpoint(od))
> library(sf)
> bench::mark(check = FALSE, max_iterations = 1,
+             journey = (r1 <<- route(l = od, route_fun = journey, plan = "quietest")),
+             journey2 = (r2 <<- journey2(fromPlace, toPlace, id = od$route_id, plan = "quietest", segments = TRUE))
+             )
# A tibble: 2 × 13
  expression      min   median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result memory     time       gc      
  <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list> <list>     <list>     <list>  
1 journey       9.16s    9.16s     0.109    20.8MB    0.328     1     3      9.16s <NULL> <Rprofmem> <bench_tm> <tibble>
2 journey2      4.73s    4.73s     0.211    24.4MB    0.423     1     2      4.73s <NULL> <Rprofmem> <bench_tm> <tibble>
Warning message:
Some expressions had a GC in every iteration; so filtering is disabled. 
Robinlovelace commented 1 year ago

Quick win to speed-up reading:

https://github.com/cyclestreets/cyclestreets-r/blob/9449bec40460adb8080c83ab7011d038faf84836/R/journey.R#L306-L325