I maintain an R package that needs to check the existence of lots of little files individually. Repeated calls to file.exists()
produce noticeable slowness ( benchmarking results here ). Unfortunately, situational constraints prevent me from calling file.exists()
once on the entire batch of files in vectorized fashion, which I believe would be a lot faster. Is there a faster way to check for the existence of a single file? Maybe in C? This way does not seem to be any faster on my system (the same one that produced these benchmarks ):
library(inline)
library(microbenchmark)
body <- "
FILE *fp = fopen(CHAR(STRING_ELT(r_path, 0)), \"r\");
SEXP result = PROTECT(allocVector(INTSXP, 1));
INTEGER(result)[0] = fp == NULL? 0 : 1;
UNPROTECT(1);
return result;
"
file_exists_c <- cfunction(sig = signature(r_path = "character"), body = body)
tmp <- tempfile()
microbenchmark(
c = file_exists_c(tmp),
r = file.exists(tmp)
)
#> Unit: microseconds
#> expr min lq mean median uq max neval
#> c 4.912 5.0230 5.42443 5.0605 5.1240 25.264 100
#> r 3.972 4.0525 4.32615 4.1835 4.2675 11.750 100
file.create(tmp)
#> [1] TRUE
microbenchmark(
c = file_exists_c(tmp),
r = file.exists(tmp)
)
#> Unit: microseconds
#> expr min lq mean median uq max neval
#> c 16.212 16.6245 17.04727 16.7645 16.9860 32.207 100
#> r 6.242 6.4175 7.16057 7.2830 7.4605 26.781 100
Created on 2019-12-06 by the reprex package (v0.3.0)
access()
access()
does appear to be faster, but not by very much.
library(inline)
library(microbenchmark)
body <- "
SEXP result = PROTECT(allocVector(INTSXP, 1));
INTEGER(result)[0] = access(CHAR(STRING_ELT(r_path, 0)), 0)? 0 : 1;
UNPROTECT(1);
return result;
"
file_exists_c <- cfunction(
sig = signature(r_path = "character"),
body = body,
includes = "#include <unistd.h>"
)
tmp <- tempfile()
microbenchmark(
c = file_exists_c(tmp),
r = file.exists(tmp)
)
#> Unit: microseconds
#> expr min lq mean median uq max neval
#> c 1.033 1.048 1.21334 1.0745 1.0910 13.793 100
#> r 1.051 1.068 1.19280 1.0930 1.1175 10.048 100
file.create(tmp)
#> [1] TRUE
microbenchmark(
c = file_exists_c(tmp),
r = file.exists(tmp)
)
#> Unit: microseconds
#> expr min lq mean median uq max neval
#> c 1.073 1.0910 1.33543 1.1285 1.1500 16.676 100
#> r 1.172 1.1965 1.32934 1.2335 1.2695 9.916 100
Created on 2019-12-07 by the reprex package (v0.3.0)
Here is the entirety of file.exists
source code (as of this writing):
SEXP attribute_hidden do_fileexists(SEXP call, SEXP op, SEXP args, SEXP rho)
{
SEXP file, ans;
int i, nfile;
checkArity(op, args);
if (!isString(file = CAR(args)))
error(_("invalid '%s' argument"), "file");
nfile = LENGTH(file);
ans = PROTECT(allocVector(LGLSXP, nfile));
for (i = 0; i < nfile; i++) {
LOGICAL(ans)[i] = 0;
if (STRING_ELT(file, i) != NA_STRING) {
#ifdef Win32
/* Package XML sends arbitrarily long strings to file.exists! */
size_t len = strlen(CHAR(STRING_ELT(file, i)));
if (len > MAX_PATH)
LOGICAL(ans)[i] = FALSE;
else
LOGICAL(ans)[i] =
R_WFileExists(filenameToWchar(STRING_ELT(file, i), TRUE));
#else
// returns NULL if not translatable
const char *p = translateCharFP2(STRING_ELT(file, i));
LOGICAL(ans)[i] = p && R_FileExists(p);
#endif
} else LOGICAL(ans)[i] = FALSE;
}
UNPROTECT(1); /* ans */
return ans;
}
As for R_FileExists
, it's here:
#ifdef Win32
Rboolean R_FileExists(const char *path)
{
struct _stati64 sb;
return _stati64(R_ExpandFileName(path), &sb) == 0;
}
#else
Rboolean R_FileExists(const char *path)
{
struct stat sb;
return stat(R_ExpandFileName(path), &sb) == 0;
}
( R_ExpandFileName
is just doing path.expand
). It's relying on the stat
system utility:
https://en.wikipedia.org/wiki/Stat_(system_call)
https://pubs.opengroup.org/onlinepubs/007908799/xsh/sysstat.h.html
It's built for vectorized inputs, so as mentioned it's much preferable to do file.exists(vector_of_files)
than to repeatedly run file.exists(single_file)
.
From what I can tell (admittedly I'm no expert on the system utilities here), any efficiency gains come at the cost of robustness.
A simple solution in C would be to use access( name of file , 0); if the function returns 0 then the file exists. The second parameter 0 specifies check only if it exists. Example: I check for the file test.txt in /test directory
#include "io.h"
#include "stdio.h"
int main()
{
if(!access("/test/test.txt",0)) printf("file exists");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.