Skip to content

Instantly share code, notes, and snippets.

@shabbychef
Created May 31, 2017 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shabbychef/4a2e85844224613893f01e366d4b340f to your computer and use it in GitHub Desktop.
Save shabbychef/4a2e85844224613893f01e366d4b340f to your computer and use it in GitHub Desktop.
Compare Rolls Again
options(width=160)
suppressMessages({
library(fromo)
library(roll)
library(RcppRoll)
library(microbenchmark)
library(RollingWindow)
})
show(sessionInfo())
set.seed(1234)
df <- data.frame(Adjusted=rnorm(10000))
matdf <- as.matrix(df$Adjusted)
wins <- 50
res <- microbenchmark(RcppRoll::roll_mean(df$Adjusted,n=wins,align='right',fill=NA),
roll::roll_mean(as.matrix(df$Adjusted), width=wins),
roll::roll_mean(matdf, width=wins),
RollingWindow::RollingMean(df$Adjusted, window=wins),
fromo::running_mean(df$Adjusted,window=wins,restart_period=10000L,na_rm=FALSE)
)
print(res)
# and again with 1 thread:
library(RcppParallel)
RcppParallel::setThreadOptions(numThreads = 1)
res <- microbenchmark(RcppRoll::roll_mean(df$Adjusted,n=wins,align='right',fill=NA),
roll::roll_mean(as.matrix(df$Adjusted), width=wins),
roll::roll_mean(matdf, width=wins),
RollingWindow::RollingMean(df$Adjusted, window=wins),
fromo::running_mean(df$Adjusted,window=wins,restart_period=10000L,na_rm=FALSE)
)
print(res)
# runtime should be independent of window size:
testme <- function(wins) { RcppRoll::roll_mean(df$Adjusted,n=wins,align='right',fill=NA) }
res <- microbenchmark(testme(10),
testme(100),
testme(1000))
print(res)
testme <- function(wins) { roll::roll_mean(as.matrix(df$Adjusted), width=wins) }
res <- microbenchmark(testme(10),
testme(100),
testme(1000))
print(res)
testme <- function(wins) { RollingWindow::RollingMean(df$Adjusted, window=wins) }
res <- microbenchmark(testme(10),
testme(100),
testme(1000))
print(res)
testme <- function(wins) { fromo::running_sd3(df$Adjusted,window=wins,restart_period=10000L,na_rm=FALSE)[,2] }
res <- microbenchmark(testme(10),
testme(100),
testme(1000))
print(res)
R version 3.3.2 (2016-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8
[6] LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] methods stats utils base
other attached packages:
[1] RollingWindow_0.2 microbenchmark_1.4-2.1 RcppRoll_0.2.2 roll_1.0.7 fromo_0.1.3.3330 Quandl_2.8.0
[7] xts_0.9-7 zoo_1.7-12 drat_0.1.2
loaded via a namespace (and not attached):
[1] Rcpp_0.12.10 lattice_0.20-33 grDevices_3.3.2 plyr_1.8.4 grid_3.3.2 R6_2.2.0 jsonlite_1.4
[8] gtable_0.2.0 scales_0.4.1 RcppParallel_4.3.20 ggplot2_2.2.1 httr_1.2.1 lazyeval_0.2.0 graphics_3.3.2
[15] tools_3.3.2 munsell_0.4.3 colorspace_1.3-2 tibble_1.3.0
Unit: microseconds
expr min lq mean median uq max neval cld
RcppRoll::roll_mean(df$Adjusted, n = wins, align = "right", fill = NA) 322.42 350.20 370.48 360.49 376.24 586.9 100 c
roll::roll_mean(as.matrix(df$Adjusted), width = wins) 395.35 409.88 460.07 420.54 451.53 1367.7 100 d
roll::roll_mean(matdf, width = wins) 333.89 369.15 410.55 380.20 395.86 1301.9 100 c
RollingWindow::RollingMean(df$Adjusted, window = wins) 158.18 169.63 213.23 186.45 197.12 1156.5 100 b
fromo::running_mean(df$Adjusted, window = wins, restart_period = 10000L, na_rm = FALSE) 67.17 73.24 94.79 80.98 95.26 968.8 100 a
Unit: microseconds
expr min lq mean median uq max neval cld
RcppRoll::roll_mean(df$Adjusted, n = wins, align = "right", fill = NA) 296.50 301.35 322.0 305.20 315.47 599.7 100 c
roll::roll_mean(as.matrix(df$Adjusted), width = wins) 864.41 868.92 916.9 874.60 904.18 2062.9 100 d
roll::roll_mean(matdf, width = wins) 842.19 846.24 878.6 849.39 871.29 1901.5 100 d
RollingWindow::RollingMean(df$Adjusted, window = wins) 142.49 147.81 207.5 150.36 161.82 1241.4 100 b
fromo::running_mean(df$Adjusted, window = wins, restart_period = 10000L, na_rm = FALSE) 60.37 64.75 68.5 66.16 68.49 101.7 100 a
Unit: microseconds
expr min lq mean median uq max neval cld
testme(10) 75.47 83.92 92.09 90.62 95.76 163.5 100 a
testme(100) 613.74 617.37 664.31 632.50 669.83 1091.8 100 b
testme(1000) 6922.48 7107.21 7322.85 7223.01 7530.08 8577.1 100 c
Unit: microseconds
expr min lq mean median uq max neval cld
testme(10) 242.9 256 294.2 270.9 303.5 1340 100 a
testme(100) 1587.1 1665 1750.4 1718.7 1774.9 2909 100 b
testme(1000) 14307.4 14720 15271.9 14988.2 15481.4 18745 100 c
Unit: microseconds
expr min lq mean median uq max neval cld
testme(10) 145.2 149.8 221.3 155.6 178.6 1283.9 100 ab
testme(100) 142.1 150.2 233.7 156.0 171.9 1587.4 100 b
testme(1000) 139.1 146.8 162.7 152.4 166.8 322.9 100 a
Unit: microseconds
expr min lq mean median uq max neval cld
testme(10) 487.8 543.9 622.3 571.0 630.0 2197 100 a
testme(100) 481.1 548.8 691.2 587.7 635.1 2771 100 a
testme(1000) 498.3 536.6 613.0 570.2 629.4 2790 100 a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment