Skip to content

Commit 4fad0ba

Browse files
committed
exists: benchmark java.io.file vs java.nio.file
1 parent 63b9a1a commit 4fad0ba

File tree

6 files changed

+27
-8
lines changed

6 files changed

+27
-8
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/create_symlink.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
% ok = java.nio.file.Files.createSymbolicLink(java.io.File(link).toPath(), java.io.File(target).toPath());
2020
% the above should work, but Matlab Java doesn't recognize the optional argument omitted.
2121

22-
if stdlib.exists(link)
22+
if stdlib.exists(link, stdlib.has_java())
2323
ok = false;
2424
return
2525
end

+stdlib/exists.m

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
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
1211
p (1,1) string
13-
use_java (1,1) logical = false
12+
use_java (1,1) logical = true
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+
% about the same speed as 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");
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
@@ -52,7 +52,7 @@ function setup_path(tc)
5252
methods (Test, ParameterCombination = 'sequential')
5353

5454
function test_exists(tc, p_exists)
55-
tc.verifyEqual(stdlib.exists(p_exists{1}), p_exists{2})
55+
tc.verifyEqual(stdlib.exists(p_exists{1}, stdlib.has_java()), p_exists{2})
5656
end
5757

5858

0 commit comments

Comments
 (0)