Skip to content

Commit b48bf84

Browse files
committed
exists,is{read,writ}able: use intrinsic vs. Java for 10x speedup
1 parent 2a3b573 commit b48bf84

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

+stdlib/exists.m

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
function ok = exists(p)
1+
function ok = exists(p, use_java)
22
%% exists does path exist
33
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#exists()
44

55
arguments
66
p (1,1) string
7+
use_java (1,1) logical = false
8+
end
9+
10+
if use_java
11+
% Java takes 2x to 10x as long as intrinsic way worst case
12+
% the intrinsic way above is at least not slower
13+
14+
ok = java.io.File(p).exists();
15+
else
16+
ok = isfile(p) || isfolder(p);
717
end
818

9-
ok = java.io.File(p).exists();
1019

1120
end

+stdlib/is_readable.m

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
function ok = is_readable(file)
1+
function ok = is_readable(file, use_jvm)
22
%% is_readable is file readable
33
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isReadable(java.nio.file.Path)
44

55
arguments
66
file (1,1) string
7+
use_jvm (1,1) logical = false
78
end
89

9-
% needs absolute()
10-
file = stdlib.absolute(file);
10+
if use_jvm
11+
% java is about 10x slower than fileattrib
12+
% needs absolute()
13+
file = stdlib.absolute(file);
1114

12-
ok = java.nio.file.Files.isReadable(java.io.File(file).toPath());
15+
ok = java.nio.file.Files.isReadable(java.io.File(file).toPath());
16+
else
17+
[status, v] = fileattrib(file);
18+
19+
ok = status ~= 0 && (v.UserRead || (~isnan(v.GroupRead) && v.GroupRead) || (~isnan(v.OtherRead) && v.OtherRead));
20+
end
1321

1422
end

+stdlib/is_writable.m

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
function ok = is_writable(file)
1+
function ok = is_writable(file, use_jvm)
22
%% is_writable
33
% is path writable
44
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/Files.html#isWritable(java.nio.file.Path)
55

66
arguments
77
file (1,1) string
8+
use_jvm (1,1) logical = false
89
end
910

10-
% needs absolute()
11-
file = stdlib.absolute(file);
11+
if use_jvm
12+
% java is about 10x slower than fileattrib
13+
% needs absolute()
14+
file = stdlib.absolute(file);
1215

13-
ok = java.nio.file.Files.isWritable(java.io.File(file).toPath());
16+
ok = java.nio.file.Files.isWritable(java.io.File(file).toPath());
17+
else
18+
[status, v] = fileattrib(file);
19+
20+
ok = status ~= 0 && (v.UserWrite || (~isnan(v.GroupWrite) && v.GroupWrite) || (~isnan(v.OtherWrite) && v.OtherWrite));
21+
end
1422

1523
end

0 commit comments

Comments
 (0)