当我通过我们的公司网络访问文件时,我遇到了一些R问题变得非常迟缓的问题.所以,我回落,并做了一些测试,我感到非常震惊地发现,将R file.copy()命令MUCH比使用等效文件拷贝速度较慢system(mv ...).这是一个已知问题还是我在这里做错了什么?
这是我的测试:
我有3个文件:
large_random.txt - ~100MB medium_random.txt - ~10MB small_random.txt - ~1 MB我在我的Mac上创建了这些:
dd if=/dev/urandom of=small_random.txt bs=1048576 count=1
dd if=/dev/urandom of=medium_random.txt bs=1048576 count=10
dd if=/dev/urandom of=large_random.txt bs=1048576 count=100
Run Code Online (Sandbox Code Playgroud)
但是以下R测试都是使用在虚拟机中运行的Windows完成的.J驱车是当地的,N驱车距离酒店有700英里.
library(tictoc)
test_copy <- function(source, des){
tic('r file.copy')
file.remove(des)
file.copy(source, des )
toc()
tic('system call')
system(paste('rm', des, sep=' '))
system(paste('cp', source, des, sep=' '))
toc()
}
source <- 'J:\\tidy_examples\\dummyfiles\\small_random.txt'
des <- 'N:\\JAL\\2018\\_temp\\small_random.txt'
test_copy(source, des)
source <- 'J:\\tidy_examples\\dummyfiles\\medium_random.txt'
des <- 'N:\\JAL\\2018\\_temp\\medium_random.txt'
test_copy(source, des)
source <- 'J:\\tidy_examples\\dummyfiles\\large_random.txt'
des <- 'N:\\JAL\\2018\\_temp\\large_random.txt'
test_copy(source, des)
Run Code Online (Sandbox Code Playgroud)
其结果如下:
> source <- 'J:\\tidy_examples\\dummyfiles\\small_random.txt'
> des <- 'N:\\JAL\\2018\\_temp\\small_random.txt'
> test_copy(source, des)
r file.copy: 6.49 sec elapsed
system call: 2.12 sec elapsed
>
> source <- 'J:\\tidy_examples\\dummyfiles\\medium_random.txt'
> des <- 'N:\\JAL\\2018\\_temp\\medium_random.txt'
> test_copy(source, des)
r file.copy: 56.86 sec elapsed
system call: 4.65 sec elapsed
>
> source <- 'J:\\tidy_examples\\dummyfiles\\large_random.txt'
> des <- 'N:\\JAL\\2018\\_temp\\large_random.txt'
> test_copy(source, des)
r file.copy: 562.94 sec elapsed
system call: 31.01 sec elapsed
>
Run Code Online (Sandbox Code Playgroud)
那么是什么让系统调用更快?在大文件大小,它慢了18倍!
小智 5
我在网络共享驱动器上的 file.copy 性能低下遇到了同样的问题。我的解决方案是使用 fs::file_copy() 代替,它的性能甚至比直接系统调用 copy 还要好一点。https://www.rdocumentation.org/packages/fs/versions/1.3.1/topics/copy