Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up routing #46

Closed
Robinlovelace opened this issue Feb 8, 2023 · 3 comments
Closed

Speed up routing #46

Robinlovelace opened this issue Feb 8, 2023 · 3 comments

Comments

@Robinlovelace
Copy link
Collaborator

As raised by @mem48 here nptscot#1

@Robinlovelace
Copy link
Collaborator Author

atumscott added a commit to nptscot/cyclestreets-r that referenced this issue Feb 16, 2023
atumscott added a commit to nptscot/cyclestreets-r that referenced this issue Feb 16, 2023
atumscott added a commit to nptscot/npt that referenced this issue Feb 16, 2023
@Robinlovelace
Copy link
Collaborator Author

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
Copy link
Collaborator Author

Quick win to speed-up reading:

cyclestreets-r/R/journey.R

Lines 306 to 325 in 9449bec

vals_variable$elevation_mean = stringr::str_split(vals_variable$elevations, pattern = ",") %>%
lapply(as.numeric) %>%
lapply(mean) %>%
unlist()
vals_variable$elevation_start = stringr::str_split(vals_variable$elevations, pattern = ",") %>%
lapply(as.numeric) %>%
lapply(utils::head, 1) %>%
unlist()
vals_variable$elevation_end = stringr::str_split(vals_variable$elevations, pattern = ",") %>%
lapply(as.numeric) %>%
lapply(utils::tail, 1) %>%
unlist()
vals_variable$elevation_max = stringr::str_split(vals_variable$elevations, pattern = ",") %>%
lapply(as.numeric) %>%
lapply(max) %>%
unlist()
vals_variable$elevation_min = stringr::str_split(vals_variable$elevations, pattern = ",") %>%
lapply(as.numeric) %>%
lapply(min) %>%
unlist()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant