data.table - benchmark, using functions on a column



boltsdt <- data.table(structure(list(RUN = c(25, 24, 30, 2, 40, 37, 16, 22, 33, 17, 28, 27, 14, 13, 4, 21, 23, 35, 19, 34, 31, 9, 38, 15, 39, 8, 26, 11, 6, 20, 10, 32, 1, 3, 5, 7, 12, 18, 29, 36), SPEED1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2", "4", "6"), class = "factor"), TOTAL = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("10", "20", "30"), class = "factor"), SPEED2 = structure(c(1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1.5", "2", "2.5"), class = "factor"), NUMBER2 = structure(c(1L, 1L, 3L, 3L, 1L, 1L, 3L, 3L, 1L, 1L, 3L, 3L, 1L, 1L, 3L, 3L, 1L, 1L, 3L, 3L, 1L, 1L, 3L, 3L, 1L, 1L, 3L, 3L, 1L, 1L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("0", "1", "2"), class = "factor"), SENS = structure(c(2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("0", "6", "8", "10"), class = "factor"), TIME = c(5.7, 17.56, 11.28, 8.39, 16.67, 12.04, 9.22, 3.94, 27.02, 19.46, 18.54, 25.7, 19.02, 22.39, 23.85, 30.12, 13.42, 34.26, 39.74, 10.6, 28.89, 35.61, 17.2, 6, 129.45, 107.38, 111.66, 109.1, 100.43, 109.28, 106.46, 134.01, 10.78, 9.39, 9.84, 13.94, 12.33, 7.32, 7.91, 15.58), T20BOLT = c(11.4, 35.12, 22.56, 16.78, 33.34, 24.08, 18.44, 7.88, 18.01, 12.97, 12.36, 17.13, 12.68, 14.93, 15.9, 20.08, 26.84, 68.52, 79.48, 21.2, 57.78, 71.22, 34.4, 12, 86.3, 71.59, 74.44, 72.73, 66.95, 72.85, 70.97, 89.34, 10.78, 9.39, 9.84, 13.94, 12.33, 7.32, 7.91, 15.58)), .Names = c("RUN", "SPEED1", "TOTAL", "SPEED2", "NUMBER2", "SENS", "TIME", "T20BOLT"), row.names = c(NA, -40L), class = c("data.table", "data.frame")))

# NOTE that boltsdt has no key
stopifnot(is.null(key(boltsdt)))  

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# without key
benchmark(
  dtWay = boltsdt[, quantile(T20BOLT, probs=c(trimPercent, 1-trimPercent), na.rm = TRUE)], 
  classic = quantile(boltsdt[["T20BOLT"]], probs=c(trimPercent, 1-trimPercent), na.rm = TRUE), 
  replications=1000, columns=c("test", "relative", "elapsed", "user.self", "sys.self"), order="relative")

  #       test relative elapsed user.self sys.self
  #  2 classic    1.000   0.315     0.315    0.002
  #  1   dtWay    2.765   0.871     0.864    0.010


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# with key, but unrelated
setkey(boltsdt, RUN)
benchmark(
  dtWay = boltsdt[, quantile(T20BOLT, probs=c(trimPercent, 1-trimPercent), na.rm = TRUE)], 
  classic = quantile(boltsdt[["T20BOLT"]], probs=c(trimPercent, 1-trimPercent), na.rm = TRUE), 
  replications=1000, columns=c("test", "relative", "elapsed", "user.self", "sys.self"), order="relative")

  #       test relative elapsed user.self sys.self
  #  2 classic    1.000   0.270     0.270    0.001
  #  1   dtWay    3.244   0.876     0.869    0.009

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# with key, on column
setkey(boltsdt, T20BOLT)
benchmark(
  dtWay = boltsdt[, quantile(T20BOLT, probs=c(trimPercent, 1-trimPercent), na.rm = TRUE)], 
  classic = quantile(boltsdt[["T20BOLT"]], probs=c(trimPercent, 1-trimPercent), na.rm = TRUE), 
  replications=1000, columns=c("test", "relative", "elapsed", "user.self", "sys.self"), order="relative")

  #       test relative elapsed user.self sys.self
  #  2 classic    1.000   0.272     0.268    0.004
  #  1   dtWay    3.239   0.881     0.872    0.013


###########################################################################################
###########################################################################################


for (i in 1:10)
  boltsdt <- rbind(boltsdt, boltsdt, boltsdt, boltsdt, boltsdt, boltsdt, boltsdt)

# > dim(boltsdt)
# [1] 4705960    8

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# with key, but unrelated
setkey(boltsdt, RUN)
benchmark(
  dtWay = boltsdt[, quantile(T20BOLT, probs=c(trimPercent, 1-trimPercent), na.rm = TRUE)], 
  classic = quantile(boltsdt[["T20BOLT"]], probs=c(trimPercent, 1-trimPercent), na.rm = TRUE), 
  replications=10, columns=c("test", "relative", "elapsed", "user.self", "sys.self"), order="relative")

  #       test relative elapsed user.self sys.self
  #  2 classic    1.000   3.015     2.713    0.315
  #  1   dtWay    1.151   3.471     3.041    0.447

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# with key, on column
setkey(boltsdt, T20BOLT)
benchmark(
  dtWay = boltsdt[, quantile(T20BOLT, probs=c(trimPercent, 1-trimPercent), na.rm = TRUE)], 
  classic = quantile(boltsdt[["T20BOLT"]], probs=c(trimPercent, 1-trimPercent), na.rm = TRUE), 
  replications=10, columns=c("test", "relative", "elapsed", "user.self", "sys.self"), order="relative")

  #       test relative elapsed user.self sys.self
  #  2 classic    1.000   2.731     2.453    0.293
  #  1   dtWay    1.172   3.200     2.711    0.512



###########################################################################################
###########################################################################################
