Skip to content

Commit abf74b0

Browse files
committed
exists: benchmark java.io.file vs java.nio.file
test:exists: add relative file case
1 parent 63b9a1a commit abf74b0

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

+stdlib/canonical.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
return
3232
end
3333

34-
e = stdlib.exists(c);
34+
e = stdlib.exists(c, use_java);
3535

3636
if ~stdlib.is_absolute(c)
3737
if e

+stdlib/exists.m

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
%%% Outputs
66
% * ok: true if exists
77
%
8-
% Ref: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#exists()
98

109
function ok = exists(p, use_java)
1110
arguments
@@ -14,9 +13,14 @@
1413
end
1514

1615
if use_java
17-
% Java takes 2x to 10x as long as intrinsic way worst case
18-
% the intrinsic way above is at least not slower
1916

17+
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#exists(java.nio.file.Path,java.nio.file.LinkOption...)
18+
% this takes 2x longer than java.io.File.exists()
19+
% opt = java.nio.file.LinkOption.values;
20+
% ok = java.nio.file.Files.exists(java.io.File(p).toPath(), opt);
21+
22+
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#exists()
23+
% takes 2x longer than native Matlab isfile || isfolder
2024
ok = java.io.File(p).exists();
2125
else
2226
ok = isfile(p) || isfolder(p);

+stdlib/samepath.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
end
1818

1919
issame = false;
20-
if ~stdlib.exists(path1) || ~stdlib.exists(path2)
20+
if ~stdlib.exists(path1, use_java) || ~stdlib.exists(path2, use_java)
2121
return
2222
end
2323

example/bench_exists.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%% benchmark for exists()
2+
3+
f = mfilename("fullpath") + ".m";
4+
%f = tempname;
5+
6+
fno = @() stdlib.exists(f, false);
7+
fjava = @() stdlib.exists(f, true);
8+
9+
t_no = timeit(fno);
10+
t_java = timeit(fjava);
11+
12+
disp("No Java: " + t_no + " s")
13+
disp("Java: " + t_java + " s")
14+
15+
disp("Java is " + t_no/t_java + " times faster")

test/TestFileImpure.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
end
66

77
properties(TestParameter)
8-
p_exists = {{pwd(), true}, {mfilename("fullpath") + ".m", true}, {tempname, false}}
8+
p_exists = {{pwd(), true}, {mfilename("fullpath") + ".m", true}, {stdlib.filename(mfilename("fullpath") + ".m"), true} {tempname, false}}
99
% on CI matlabroot can be writable!
1010
in_is_write = {pwd(), "not-exists"};
1111
ref_is_write = {true, false}

0 commit comments

Comments
 (0)