我将重申fread 明显更快,如 Stack Overflow 上的这篇文章所示:Quickly reading very large tables as dataframes in R。总而言之,测试(在 51 Mb 文件上 - 1e6 行 x 6 列)显示与最佳替代方法(包括sqldf、ff 和read.table)相比,无论是否使用推荐的优化设置,性能提高了 70% 以上在@lukeA 的回答中。这在 cmets 中得到了备份,它报告使用 fread 在不到一分钟的时间内加载 4GB 文件,而使用基本函数则需要 15 小时。
我自己进行了一些测试,以比较读取和组合 CSV 文件的替代方法。实验设置如下:
- 为每次运行生成 4 列 CSV 文件(
character x 1、numeric x 3)。共有 6 次运行,每次运行的行数不同,范围为数据文件中的 10^1、10^2、...、10^6 记录。
- 将 CSV 文件导入
R 10 次,与rbind 或rbindlist 连接以创建单个表。
- 针对
fread 测试read.csv 和read.table,使用和不使用优化参数(如colClasses)。
- 使用
microbenchmark 重复每个测试 10 次(可能不必要地高!),并收集每次运行的时间。
结果再次显示支持fread 和rbindlist 超过优化read.table 和rbind 功能。
此表显示了median 每种方法的 10 次文件读取和组合的总持续时间以及每个文件的行数。前 3 列以微秒为单位,后 3 列以秒为单位。
expr 10 100 1000 10000 1e+05 1e+06
1: FREAD 3.93704 5.229699 16.80106 0.1470289 1.324394 12.28122
2: READ.CSV 12.38413 18.887334 78.68367 0.9609491 8.820387 187.89306
3: READ.CSV.PLUS 10.24376 14.480308 60.55098 0.6985101 5.728035 51.83903
4: READ.TABLE 12.82230 21.019998 74.49074 0.8096604 9.420266 123.53155
5: READ.TABLE.PLUS 10.12752 15.622499 57.53279 0.7150357 5.715737 52.91683
此图显示了在 HPC 上运行 10 次时的时序比较:
根据fread 时间对这些值进行标准化显示这些其他方法在所有情况下需要多长时间:
10 100 1000 10000 1e+05 1e+06
FREAD 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
READ.CSV 3.145543 3.611553 4.683256 6.535784 6.659941 15.299223
READ.CSV.PLUS 2.601893 2.768861 3.603998 4.750835 4.325023 4.221001
READ.TABLE 3.256838 4.019352 4.433693 5.506811 7.112887 10.058576
READ.TABLE.PLUS 2.572370 2.987266 3.424355 4.863232 4.315737 4.308762
HPC 上 10 次 microbenchmark 迭代的结果表
有趣的是,对于每个文件 100 万行,read.csv 和 read.table 的优化版本比 fread 多花费 422% 和 430% 的时间,而在没有优化的情况下,这一时间跃升至大约 1500% 和 1005%。
请注意,当我在功能强大的笔记本电脑上(而不是 HPC 集群)进行此实验时,性能提升略少(慢了大约 81%,而不是慢了 400%)。这本身就很有趣,但我不确定我能解释清楚!
10 100 1000 10000 1e+05 1e+06
FREAD 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
READ.CSV 2.595057 2.166448 2.115312 3.042585 3.179500 6.694197
READ.CSV.PLUS 2.238316 1.846175 1.659942 2.361703 2.055851 1.805456
READ.TABLE 2.191753 2.819338 5.116871 7.593756 9.156118 13.550412
READ.TABLE.PLUS 2.275799 1.848747 1.827298 2.313686 1.948887 1.832518
Table of results for only 5 `microbenchmark` iterations on my i7 laptop
鉴于数据量相当大,我建议好处不仅在于使用fread 读取文件,而且在于随后使用data.table 包而不是传统的@ 包处理数据987654356@操作!我很幸运能够在早期阶段吸取这一教训,并会推荐其他人效仿......
这是测试中使用的代码。
rm(list=ls()) ; gc()
library(data.table) ; library(microbenchmark)
#=============== FUNCTIONS TO BE TESTED ===============
f_FREAD = function(NUM_READS) {
for (i in 1:NUM_READS) {
if (i == 1) x = fread("file.csv") else x = rbindlist(list(x, fread("file.csv")))
}
}
f_READ.TABLE = function(NUM_READS) {
for (i in 1:NUM_READS) {
if (i == 1) x = read.table("file.csv") else x = rbind(x, read.table("file.csv"))
}
}
f_READ.TABLE.PLUS = function (NUM_READS) {
for (i in 1:NUM_READS) {
if (i == 1) {
x = read.table("file.csv", sep = ",", header = TRUE, comment.char="", colClasses = c("character", "numeric", "numeric", "numeric"))
} else {
x = rbind(x, read.table("file.csv", sep = ",", header = TRUE, comment.char="", colClasses = c("character", "numeric", "numeric", "numeric")))
}
}
}
f_READ.CSV = function(NUM_READS) {
for (i in 1:NUM_READS) {
if (i == 1) x = read.csv("file.csv") else x = rbind(x, read.csv("file.csv"))
}
}
f_READ.CSV.PLUS = function (NUM_READS) {
for (i in 1:NUM_READS) {
if (i == 1) {
x = read.csv("file.csv", header = TRUE, colClasses = c("character", "numeric", "numeric", "numeric"))
} else {
x = rbind(x, read.csv("file.csv", comment.char="", header = TRUE, colClasses = c("character", "numeric", "numeric", "numeric")))
}
}
}
#=============== MAIN EXPERIMENTAL LOOP ===============
for (i in 1:6)
{
NUM_ROWS = (10^i) # the loop allows us to test the performance over varying numbers of rows
NUM_READS = 10
# create a test data.table with the specified number of rows and write it to file
dt = data.table(
col1 = sample(letters[],NUM_ROWS,replace=TRUE),
col2 = rnorm(NUM_ROWS),
col3 = rnorm(NUM_ROWS),
col4 = rnorm(NUM_ROWS)
)
write.csv(dt, "file.csv", row.names=FALSE)
# run the imports for each method, recording results with microbenchmark
results = microbenchmark(
FREAD = f_FREAD(NUM_READS),
READ.TABLE = f_READ.TABLE(NUM_READS),
READ.TABLE.PLUS = f_READ.TABLE.PLUS(NUM_READS),
READ.CSV = f_READ.CSV(NUM_READS),
READ.CSV.PLUS = f_READ.CSV.PLUS(NUM_READS),
times = NUM_ITERATIONS)
results = data.table(NUM_ROWS = NUM_ROWS, results)
if (i == 1) results.all = results else results.all = rbindlist(list(results.all, results))
}
results.all[,time:=time/1000000000] # convert from nanoseconds