ropensci / stplanr

Sustainable transport planning with R
https://docs.ropensci.org/stplanr
Other
416 stars 66 forks source link

`line_bearing()` is slow #551

Closed Robinlovelace closed 9 months ago

Robinlovelace commented 9 months ago

Another 10x+ speed-up, FYI @mem48, @JosiahParry, @palelimbot, @kadyb and anyone else interested in perf. What lwgeom slowness doing and worth reporting upstream?

> l <- flowlines_sf[1:5, ]
> bearings_sf_1_9 <- line_bearing(l)
> bearings_sf_1_9 # lines of 0 length have NaN bearing
[1]  -92.04942 -102.44566 -143.83660 -173.33154  170.51696
> line_bearing(l, bidirectional = TRUE)
[1] 87.950582 77.554344 36.163396  6.668455 -9.483044
> # benchmark:
> profvis::profvis(line_bearing(l))
> bm = bench::mark(line_bearing(l))
> l <- flowlines_sf[1:5, ]
> bearings_sf_1_9 <- line_bearing(l)
> bearings_sf_1_9 # lines of 0 length have NaN bearing
[1]  -92.04942 -102.44566 -143.83660 -173.33154  170.51696
> line_bearing(l, bidirectional = TRUE)
[1] 87.950582 77.554344 36.163396  6.668455 -9.483044
> # benchmark:
> # profvis::profvis(line_bearing(l))
> bm = bench::mark(line_bearing(l))
> bm2 = bench::mark(geosphere = {
+ odc = od::od_coordinates(l)
+ geosphere::bearing(
+   p1 = odc[, 1:2],
+   p2 = odc[, 3:4]
+ )
+ })
> bm$`itr/sec`
[1] 58.62699
> bm2$`itr/sec`
[1] 3095.411
> bm2$`itr/sec` / bm$`itr/sec`
[1] 52.79839
Robinlovelace commented 9 months ago

New implementation, same results:

> bearings_sf_1_9 # lines of 0 length have NaN bearing
[1]  -92.04942 -102.44566 -143.83660 -173.33154  170.51696
Robinlovelace commented 9 months ago

> line_bearing(l, bidirectional = TRUE)
[1] 87.950582 77.554344 36.163396  6.668455 -9.483044