Ash*_*ith 22 r data.table
我正在使用以下方法使用fread将文件读入R:
fread("file:///C:/Users/Desktop/ads.csv")
fread("C:/Users/Desktop/ads.csv") # Just omitted "file:///"
Run Code Online (Sandbox Code Playgroud)
我观察到运行时非常不同:
microbenchmark(
fread("file:///C:/Users/Desktop/ads.csv"),
fread("C:/Users/Desktop/ads.csv")
)
Unit: microseconds
expr min lq mean median uq max neval cld
fread("file:///C:/Users/Desktop/ads.csv") 5755.975 6027.4735 6696.7807 6235.3365 6506.652 41257.476 100 b
fread("C:/Users/Desktop/ads.csv") 525.492 584.0215 673.7166 647.4745 727.703 1476.191 100 a
Run Code Online (Sandbox Code Playgroud)
为什么运行时变化如此之大?当我使用read.csv()时,两个变体之间没有明显的区别
Mic*_*ico 24
以下内容已添加到?fread
:
当
input
以http://开头,https://开头,FTP://,FTPS://,或file://,fread
检测到此并下载目标到一个临时文件(在tempfile()
前进到读取文件照常之前) .下载安全URLS(ftps://和https://)curl::curl_download
; ftp://和http://路径下载download.file
并method
设置为getOption("download.file.method")
,默认为"auto"
; 和file://随download.file
with 下载method="internal"
.注意:这意味着对于file://,即使在当前机器上找到的文件也会被"下载"(即硬拷贝)到临时文件中.有关?download.file
详细信息,请参阅
从以下来源fread
:
if (str6 == "ftp://" || str7 == "http://" || str7 == "file://") {
method = if (str7 == "file://") "auto"
else getOption("download.file.method", default = "auto")
download.file(input, tmpFile, method = method, mode = "wb", quiet = !showProgress)
}
Run Code Online (Sandbox Code Playgroud)
也就是说,您的文件被"下载"到临时文件,该文件应该包括将文件内容深度复制到临时位置.file://
实际上并不打算用于本地文件,而是用于需要在本地下载之前在网络中读取的文件(IIUC; FWIW,这是fread
测试机制在CRAN上测试时模拟文件下载的用途,其中外部文件下载是不可能的).
我还注意到你的时间是微秒级,这可以解释差异与read.csv
.想象一下read.csv
需要1秒才能读取文件,同时fread
需要0.01秒; 文件复制需要.05
几秒钟.然后在两种情况下read.csv
都会看起来相同(1对1.05秒),而fread
对于这种file://
情况看起来要慢得多(.01对.06秒).