gpt4 book ai didi

r - list.files 的性能问题

转载 作者:行者123 更新时间:2023-12-04 12:35:39 25 4
gpt4 key购买 nike

我正在尝试使用 list.files 从 3 个网络驱动器中检索文件,这需要很长时间。当我在 shell 中使用 find 时,它会在不到 15 秒的时间内返回所有结果。

system.time(
jnk <- list.files(c("/Volumes/massspec", "/Volumes/massspec2", "/Volumes/massspec3"),
pattern='_MA_.*_HeLa_',
recursive=TRUE))
# user system elapsed
# 1.567 6.381 309.500

这是等效的 shell 命令。

time find /Volumes/masssp* -name *_MA_*_HeLa_*
# real 0m13.776s
# user 0m0.361s
# sys 0m0.620s

我需要一个适用于 Windows 和 Unix 系统的解决方案。有人有好主意吗?网络驱动器总共有大约 120,000 个文件,但大约 16TB。所以文件不多,但非常大。

最佳答案

根据评论,我写了一个应该在 Windows 和 Unix 上工作的小 R 函数...

quickFileSearch <- function(path, pattern) {
switch (.Platform$OS.type,
unix={
paths <- paste(path, collapse=' ')
command <- paste('find', paths, '-name', pattern)
system(command, intern=TRUE)
},
windows={
paths <- paste(file.path(path, pattern,
fsep='\\'),
collapse=' ')
command <- paste('dir', paths, '/b /s /a-d')
shell(command, intern=TRUE)}
)
}

整个事情还没有经过太多测试,但它正在为我的目的工作。

关于r - list.files 的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39743174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com