forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_with_subset.R
53 lines (38 loc) · 1.57 KB
/
bench_with_subset.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
library(microbenchmark)
library(data.table)
data.frame.subset.bench <- function (n=1e7, times=30) {
df <- data.frame(a=rnorm(n), b=rnorm(n), c=rnorm(n))
print(microbenchmark(subset(df, a <= b & b <= (c ^ 2 + b ^ 2 - a) & b > c),
times=times))
}
# data.table allows something very similar to query with an expression
# but we have chained comparisons AND we're faster BOO YAH!
data.table.subset.expression.bench <- function (n=1e7, times=30) {
dt <- data.table(a=rnorm(n), b=rnorm(n), c=rnorm(n))
print(microbenchmark(dt[, a <= b & b <= (c ^ 2 + b ^ 2 - a) & b > c],
times=times))
}
# compare against subset with data.table for good measure
data.table.subset.bench <- function (n=1e7, times=30) {
dt <- data.table(a=rnorm(n), b=rnorm(n), c=rnorm(n))
print(microbenchmark(subset(dt, a <= b & b <= (c ^ 2 + b ^ 2 - a) & b > c),
times=times))
}
data.frame.with.bench <- function (n=1e7, times=30) {
df <- data.frame(a=rnorm(n), b=rnorm(n), c=rnorm(n))
print(microbenchmark(with(df, a + b * (c ^ 2 + b ^ 2 - a) / (a * c) ^ 3),
times=times))
}
data.table.with.bench <- function (n=1e7, times=30) {
dt <- data.table(a=rnorm(n), b=rnorm(n), c=rnorm(n))
print(microbenchmark(with(dt, a + b * (c ^ 2 + b ^ 2 - a) / (a * c) ^ 3),
times=times))
}
bench <- function () {
data.frame.subset.bench()
data.table.subset.expression.bench()
data.table.subset.bench()
data.frame.with.bench()
data.table.with.bench()
}
bench()