library(data.table)
library(rbenchmark)
df = data.frame(col1 = rnorm(1000000), col2 = rnorm(1000000))
dt = data.table(df)
dt_key = copy(dt)
setkey(dt_key, col1, col2)
replications = 100
Function to test order using data.frame:
order_df_shell = function(df) {
temp = df
temp[order(temp$col1, -temp$col2),]
}
Function to test order using data.frame:
order_df_radix = function(df) {
temp = df
temp[order(temp$col1, -temp$col2, method = "radix"),]
}
Function to test order using data.table:
order_dt = function(dt) {
temp = copy(dt)
temp[order(col1, -col2), ]
}
Function to test order using data.table with index:
order_dt_key = function(dt_key) {
temp = copy(dt_key)
temp[order(col1, -col2), ]
}
Here we use rbenchmark to test above two functions
within(benchmark(order_df_shell_out <- order_df_shell(df),
order_df_radix_out <- order_df_radix(df),
order_dt_out <- order_dt(dt),
order_dt_key_out <- order_dt_key(dt_key),
replications = replications,
columns=c('test', 'replications', 'elapsed', "relative")),
{ average = elapsed/replications })
## test replications elapsed relative
## 2 order_df_radix_out <- order_df_radix(df) 100 27.598 4.508
## 1 order_df_shell_out <- order_df_shell(df) 100 267.067 43.624
## 4 order_dt_key_out <- order_dt_key(dt_key) 100 6.122 1.000
## 3 order_dt_out <- order_dt(dt) 100 14.862 2.428
## average
## 2 0.27598
## 1 2.67067
## 4 0.06122
## 3 0.14862
identical(data.table(order_df_shell_out), order_dt_out)
## [1] TRUE
sessionInfo()
## R version 3.3.0 (2016-05-03)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 14.04.4 LTS
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] rbenchmark_1.0.0 data.table_1.9.7
##
## loaded via a namespace (and not attached):
## [1] magrittr_1.5 formatR_1.3 tools_3.3.0 htmltools_0.3.5
## [5] yaml_2.1.13 Rcpp_0.12.4 stringi_1.0-1 rmarkdown_0.9.6
## [9] knitr_1.13 stringr_1.0.0 digest_0.6.9 evaluate_0.9