Skip to content

Instantly share code, notes, and snippets.

@shabbychef
Last active May 15, 2017 07:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shabbychef/04dfc10edd1cb492301b991b233788f2 to your computer and use it in GitHub Desktop.
Save shabbychef/04dfc10edd1cb492301b991b233788f2 to your computer and use it in GitHub Desktop.
compare rolling mean functions
suppressMessages({
library(fromo)
library(roll)
library(RcppRoll)
library(microbenchmark)
library(RollingWindow)
})
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_sd3(df$Adjusted,window=wins,restart_period=10000L)[,2]
)
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)[,2] }
res <- microbenchmark(testme(10),
testme(100),
testme(1000))
print(res)
Unit: microseconds
expr min lq mean median uq max neval cld
RcppRoll::roll_mean(df$Adjusted, n = wins, align = "right", fill = NA) 300.2 344.2 390.1 364.2 394.1 646.4 100 a
roll::roll_mean(as.matrix(df$Adjusted), width = wins) 384.3 430.2 514.0 445.3 473.9 1806.5 100 a
roll::roll_mean(matdf, width = wins) 347.3 398.8 463.7 414.4 444.0 2113.0 100 a
RollingWindow::RollingMean(df$Adjusted, window = wins) 146.9 165.5 609.0 188.5 210.4 32298.9 100 a
fromo::running_sd3(df$Adjusted, window = wins, restart_period = 10000L)[, 2] 430.6 496.5 638.4 520.9 556.6 1664.5 100 a
Unit: microseconds
expr min lq mean median uq max neval cld
testme(10) 79.12 90.74 141.7 100.7 110.0 1476 100 a
testme(100) 606.34 619.62 695.8 672.3 705.1 1946 100 b
testme(1000) 6932.67 7046.28 7483.6 7361.4 7792.1 8824 100 c
> source('~/comparem.r')
Unit: microseconds
expr min lq mean median uq max neval cld
RcppRoll::roll_mean(df$Adjusted, n = wins, align = "right", fill = NA) 301.5 342.9 377.3 352.5 381.4 1505 100 b
roll::roll_mean(as.matrix(df$Adjusted), width = wins) 418.1 440.3 499.1 462.3 477.5 1691 100 c
roll::roll_mean(matdf, width = wins) 395.6 413.3 443.5 429.2 442.7 1603 100 bc
RollingWindow::RollingMean(df$Adjusted, window = wins) 156.1 169.2 231.9 194.4 210.3 1684 100 a
fromo::running_sd3(df$Adjusted, window = wins, restart_period = 10000L)[, 2] 439.8 505.1 601.5 513.1 541.3 1836 100 d
Unit: microseconds
expr min lq mean median uq max neval cld
testme(10) 77.1 89.69 98.64 96.9 103.3 182.4 100 a
testme(100) 606.9 618.49 655.01 634.0 670.0 928.0 100 b
testme(1000) 6932.7 6979.20 7251.09 7068.6 7505.0 8266.8 100 c
Unit: microseconds
expr min lq mean median uq max neval cld
testme(10) 179.3 201.1 247.8 206.4 232.5 1577 100 a
testme(100) 617.8 652.1 705.2 671.9 691.3 1988 100 b
testme(1000) 4917.7 5026.7 5178.5 5063.1 5148.4 8206 100 c
Unit: microseconds
expr min lq mean median uq max neval cld
testme(10) 150.4 153.9 226.0 165.3 169.4 1387 100 a
testme(100) 142.1 152.8 242.4 156.4 172.0 1578 100 a
testme(1000) 143.2 149.7 250.0 163.0 169.5 1556 100 a
Unit: microseconds
expr min lq mean median uq max neval cld
testme(10) 438.2 493.3 524.2 496.5 501.7 1844 100 a
testme(100) 446.6 490.9 564.9 494.9 499.3 1901 100 a
testme(1000) 439.1 484.0 571.2 489.2 496.9 1839 100 a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment