baktoft / yaps

YAPS - Yet Another Positioning Solver
GNU General Public License v3.0
19 stars 4 forks source link

Possible speed up of prepDetections by using gsub #38

Closed mhpob closed 2 years ago

mhpob commented 3 years ago

Switching from sapply(x, function(x) strsplit('pattern', x)[[1]][1]) to gsub('pattern', '', x) could offer some substantial speed gains, especially when scaling up to larger data sets:

> library(yaps)
Welcome to yaps (v1.2.5)
 Please let us know if you experience any trouble using yaps. 
 Run testYaps() to ensure basic functions (incl. TMB) is working.

> fn <- system.file("extdata", "VUE_Export_ssu1.csv", package="yaps")
> vue <- data.table::fread(fn, fill=TRUE, tz = '')

> prepDetections_gsub <- function(raw_dat, type){
+   detections <- data.table::data.table()
+   if (type == "vemco_vue"){
+     detections[, ts:=as.POSIXct(raw_dat$'Date and Time (UTC)', tz="UTC")]
+     detections[, tag:=as.numeric(gsub('.*-', '', raw_dat$Transmitter))]
+     detections[, epo:=as.numeric(ts)]
+     detections[, frac:= as.numeric(gsub('.*\\.', '', raw_dat$"Date and Time (UTC)"))/1000]
+     detections[, serial:=as.numeric(gsub('.*-', '', raw_dat$Receiver))]
+   }
+   detections[]
+   return(detections)
+ }

> library(microbenchmark)
> microbenchmark(prepDetections(raw_dat=vue, type="vemco_vue"))
Unit: milliseconds
                                              expr      min       lq     mean   median       uq      max neval
 prepDetections(raw_dat = vue, type = "vemco_vue") 300.8226 323.3287 344.6861 334.9199 353.2604 743.7136   100

> microbenchmark(prepDetections_gsub(raw_dat=vue, type="vemco_vue"))
Unit: milliseconds
                                                   expr     min      lq     mean   median       uq     max neval
 prepDetections_gsub(raw_dat = vue, type = "vemco_vue") 71.2235 72.0729 73.87757 72.84115 74.53915 84.9442   100
baktoft commented 2 years ago

Solved in #39