Skip to content

Commit ae1f95e

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

File tree

6 files changed

+51
-11
lines changed

6 files changed

+51
-11
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/drop_slash.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
%% DROP_SLASH drop repeated and trailing slash
2+
3+
function d = drop_slash(p)
4+
arguments
5+
p (1,1) string
6+
end
7+
8+
s = stdlib.posix(p);
9+
10+
% drop repeated slashes inside string
11+
d = regexprep(s, "/+", "/");
12+
13+
if d == "/"
14+
return;
15+
end
16+
17+
if ~ispc || (strlength(d) ~= 3 || d ~= stdlib.root(s))
18+
d = strip(d, "right", "/");
19+
end
20+
21+
end

+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: 6 additions & 6 deletions
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}, {"TestFileImpure.m", true} {tempname, false}}
99
% on CI matlabroot can be writable!
1010
in_is_write = {pwd(), "not-exists"};
1111
ref_is_write = {true, false}
@@ -35,18 +35,18 @@
3535

3636
methods(TestClassSetup)
3737

38-
function classSetup(tc, classToTest)
39-
constructor = str2func(classToTest);
40-
tc.tobj = constructor();
41-
end
42-
4338
function setup_path(tc)
4439
import matlab.unittest.fixtures.PathFixture
4540
cwd = fileparts(mfilename("fullpath"));
4641
top = fullfile(cwd, "..");
4742
tc.applyFixture(PathFixture(top))
4843
end
4944

45+
function classSetup(tc, classToTest)
46+
constructor = str2func(classToTest);
47+
tc.tobj = constructor();
48+
end
49+
5050
end
5151

5252
methods (Test, ParameterCombination = 'sequential')

0 commit comments

Comments
 (0)