Skip to content

Conversation

@e-kotov
Copy link

@e-kotov e-kotov commented Jul 11, 2025

Hi @caesar0301 ,

I have made two faster versions of the radius of gyration function.

See below:

remotes::install_github("e-kotov/movr@faster-rgy")
library(movr)
data(movement)
lat <- movement$lat
lon <- movement$lon

bench::mark(
  radius.of.gyration(lat, lon),
  fast.radius.of.gyration(lat, lon),
  turbo.radius.of.gyration(lat, lon),
  iterations = 10,
  relative = FALSE,
  check = TRUE,
)
# A tibble: 3 × 13
  expression                           min   median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result memory     time       gc      
  <bch:expr>                         <bch> <bch:tm>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list> <list>     <list>     <list>  
1 radius.of.gyration(lat, lon)       138ms 155.44ms      6.51    3.89MB     30.0    10    46      1.53s <dbl>  <Rprofmem> <bench_tm> <tibble>
2 fast.radius.of.gyration(lat, lon)  880µs   1.09ms    583.      3.93MB     58.3    10     1    17.15ms <dbl>  <Rprofmem> <bench_tm> <tibble>
3 turbo.radius.of.gyration(lat, lon) 816µs 826.85µs   1193.    212.08KB      0      10     0     8.38ms <dbl>  <Rprofmem> <bench_tm> <tibble>
> round(radius.of.gyration(lat, lon), 6) ==
+   round(fast.radius.of.gyration(lat, lon), 6)
[1] TRUE
> round(radius.of.gyration(lat, lon), 6) ==
+   round(turbo.radius.of.gyration(lat, lon), 6)
[1] TRUE

The results match with the original function, but are 100+ times faster. The C implementation is not only faster, but also consumes just a fraction of memory. This might make a difference when analyzing many trajectories.

I am making a draft PR, as I do not suggest to add these new functions in addition to the existing one, but to replace it.

Let me know if you like any of the implementations. I guess if your package already has C code, then it makes sense to use the C function as it is still a bit faster then the fully vectorized zero-dependency R function, and is also much more memory efficient.

@e-kotov
Copy link
Author

e-kotov commented Nov 11, 2025

@caesar0301 hi, this is a kind reminder for some feedback on this PR.

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

Successfully merging this pull request may close these issues.

1 participant