Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- > ff <- function() paste0(sample(letters, sample(5:8, 1), TRUE), collapse="")
- > ll <- unique(replicate(1e4, ff()))
- > DT <- data.table(v1=sample(ll, 1e6, TRUE), v2=sample(1:1e4, 1e6, TRUE), v3=sample(1:1e4, 1e6, TRUE))
- >
- > # add functions
- > eddi <- function(dt=copy(DT)) {
- + dt[, list(v2=v2[v2 == v2[1]]), by = v1]
- + }
- >
- > andrey <- function(dt=copy(DT)) {
- + dt[, .SD[v2 == v2[1],], by = v1]
- + }
- >
- > arun <- function(dt=copy(DT)) {
- + tmp = dt[dt[, list(I=.I[1]), by=list(v1)]$I]
- + setkey(dt)[tmp]
- + }
- >
- > # benchmark
- > microbenchmark(a1 <- eddi(), a2 <- andrey(), a3 <- arun(), times=2)
- Unit: milliseconds
- expr min lq median uq max neval
- a1 <- eddi() 212 212 229 246 246 2
- a2 <- andrey() 4234 4234 4258 4282 4282 2
- a3 <- arun() 529 529 551 574 574 2
- >
- > setkey(a3, NULL)
- v1 v2 v3
- 1: ufhjypjc 2260 3859
- 2: wnraxyc 867 9698
- 3: ppvlahgr 6508 4440
- 4: eaveaoey 4396 5581
- 5: mfoifvd 2005 5205
- ---
- 9996: rjrxx 2323 7954
- 9997: hbymqhz 3723 2110
- 9998: kpiakpqh 1508 7807
- 9999: efqek 6592 5396
- 10000: assytuyp 5131 9038
- >
- > identical(a1, a2)
- [1] FALSE
- > identical(a1, a3)
- [1] FALSE
- > identical(a2, a3)
- [1] FALSE
- > all.equal(a2,a3)
- [1] "Component 1: Lengths (10094, 10000) differ (string compare on first 10000)"
- [2] "Component 1: 9740 string mismatches"
- [3] "Component 2: Numeric: lengths (10094, 10000) differ"
- [4] "Component 3: Numeric: lengths (10094, 10000) differ"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement