【问题标题】:best key type for R data.tableR data.table 的最佳键类型
【发布时间】:2016-10-25 18:28:52
【问题描述】:

整数或更小的字符串作为 data.table 中的键是否更快?例如,

 dt1 = data.table(x = c("a","b","c","d","e"), y= c(1,2,3,4,5))

 dt2 = data.table(x = c("ndjdnjndjndddjhjdhdhdbdjbjhfbdfbdfjhdbfd", "jnjwnjdndsjdsndjskndskjdnsdjsndskdnsk","jnjnsjncsccdjhcbdhjcbdcjhd","sjdnjdncjdncdcdcdccndcd","wjdndjnjcndcjdncdc"), y= c(1,2,3,4,5))

dt1 中的 x 会比 dt2.x 中的长字符串更好/更快吗?换句话说,字符串长度如何影响速度?

谢谢!

【问题讨论】:

  • 为什么不自己进行基准测试?
  • 这不是讽刺......microbenchmark 软件包使基准测试变得非常容易。
  • 在大多数问题中,我都在寻找绊脚石——是什么阻碍了提问者自己解决问题?在这个问题中,我没有看到一个绊脚石——这是一个很好的问题(不是我的反对票——只是为了平衡而赞成),但我不确定你为什么不提供答案。
  • 我同意这听起来很有趣。如果您有一个基准来显示一些差异,那么从某人那里听到“为什么”会更有趣。没必要防守。除了“只是帮助”(我认为你的意思是回答)之外,我们还想了解被问到的内容。通常,我们会看到一个要求 X 但确实有问题 Y 的问题,我们只能通过询问 OP 问题而不是立即提供答案来找到它。

标签: r data.table


【解决方案1】:

我比较了 data.table 对象在三个不同的 data.table 操作中具有不同大小的键长度的性能:

  1. 创建数据表
  2. 为 data.table 设置键
  3. 访问 data.table 中的行

代码

library(data.table)
library(random)
library(microbenchmark)

sizes = c(2, 5, 10, 20)  #Length of the strings we'll use as keys in the data.tables

# Generate random strings of different lengths:
randomstrings <- function(size){
  randomStrings(n = 100, len = size, upperalpha = F, digits = F, check = F)
}
keys <- lapply(sizes, randomstrings)  # The differently sized keys we'll use

# Create data table:
dt <- function(keys){data.table(x = keys, y = 1:100)}   

# Function that chooses 5 keys randomly (used to access lines in the benchmarking):
some5keys <- function(datatable){datatable[sample(datatable$x.V1, 5)]}

### BENCHMARKING ###
# Creating the data.tables:
(creationbench <- microbenchmark(dt1 <- dt(keys[[1]]), 
                                 dt2 <- dt(keys[[2]]), 
                                 dt3 <- dt(keys[[3]]), 
                                 dt4 <- dt(keys[[4]])))
# Unit: microseconds
# expr                 min     lq       mean     median   uq       max      neval
# dt1 <- dt(keys[[1]]) 562.926 609.1035 714.7314 672.5955 803.7075 1117.683   100
# dt2 <- dt(keys[[2]]) 565.636 605.7725 737.8285 661.0125 756.9390 5087.124   100
# dt3 <- dt(keys[[3]]) 563.347 606.8465 694.8140 631.6945 754.4420 1326.753   100
# dt4 <- dt(keys[[4]]) 578.101 622.4180 722.8112 708.4055 785.9755 1509.439   100

# Setting the keys for the data.tables:
(setkeybench <- (microbenchmark(setkey(dt1, x.V1), 
                                setkey(dt2, x.V1), 
                                setkey(dt3, x.V1), 
                                setkey(dt4, x.V1))))
# Unit: microseconds
# expr              min    lq      mean     median  uq      max       neval
# setkey(dt1, x.V1) 76.401 77.9530 82.28644 78.7440 81.3955 111.267   100
# setkey(dt2, x.V1) 75.620 77.7395 91.95130 79.6885 90.6075 343.743   100
# setkey(dt3, x.V1) 76.330 77.7900 84.21696 78.6290 83.8310 189.792   100
# setkey(dt4, x.V1) 76.044 77.8135 85.35959 79.1675 89.8920 129.458   100

# Accessing lines in the data.tables:
(selectbench <- (microbenchmark(some5keys(dt1), 
                           some5keys(dt2),
                           some5keys(dt3),
                           some5keys(dt4))))
# Unit: microseconds
# expr           min     lq       mean     median   uq       max      neval
# some5keys(dt1) 958.961 1029.778 1244.538 1131.350 1318.147 5389.407   100
# some5keys(dt2) 968.710 1037.023 1246.963 1131.209 1302.656 5890.560   100
# some5keys(dt3) 966.647 1025.569 1206.210 1140.247 1299.570 2221.324   100
# some5keys(dt4) 960.804 1042.528 1218.077 1171.347 1363.010 1813.551   100

看起来key-string的长度对data.table操作的效率绝对没有影响

请注意,您可能想要比较data.table-objects 的其他一些操作。

【讨论】:

    猜你喜欢
    • 2021-05-17
    • 1970-01-01
    • 2016-12-06
    • 2023-04-09
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    相关资源
    最近更新 更多