Skip to content

Commit cedfdbd

Browse files
committed
parent: show java way
1 parent 845aafc commit cedfdbd

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed

+stdlib/parent.m

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
%% PARENT parent directory of path
22
%
33

4-
function p = parent(pth)
4+
function p = parent(pth, use_java)
55
arguments
66
pth (1,1) string
7+
use_java (1,1) logical = false
78
end
89

9-
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getParent()
10-
% java was 10x slower and not correct for input like C:/
11-
1210
p = stdlib.drop_slash(pth);
1311

14-
if is_root_stub(p)
12+
if ~stdlib.len(p)
13+
p = ".";
14+
return
15+
elseif is_root_stub(p)
1516
% 2 or 3 char drive letter
1617
if stdlib.len(p) == 2
1718
p = strcat(p, "/");
1819
end
1920
return
21+
elseif strcmp(p, stdlib.root(p, use_java))
22+
return
2023
end
2124

25+
26+
if use_java
27+
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getParent()
28+
% Java is about 20x slower than pure Matlab
29+
p = jPosix(javaFileObject(p).getParent());
30+
31+
else
32+
2233
j = strfind(p, '/');
2334
if isempty(j)
2435
p = "";
@@ -33,13 +44,14 @@
3344
return
3445
end
3546

47+
p = stdlib.posix(p);
48+
49+
end
3650

3751
if ~stdlib.len(p)
3852
p = ".";
3953
end
4054

41-
p = stdlib.posix(p);
42-
4355
end
4456

4557

example/bench_exists.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
%% benchmark for exists()
2-
31
f = mfilename("fullpath") + ".m";
42
%f = tempname;
53

example/bench_parent.m

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

test/TestParent.m

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
classdef TestParent < matlab.unittest.TestCase
22

33
properties (TestParameter)
4-
p_parent
4+
use_java = num2cell(unique([stdlib.has_java(), false]))
5+
p
56
end
67

78
methods(TestParameterDefinition, Static)
89

9-
function p_parent = init_parent()
10+
function p = init_parent()
1011

11-
p_parent = {
12+
p = {
1213
{"", "."}, ...
1314
{".", "."}, ...
1415
{"..", "."}, ...
@@ -27,12 +28,12 @@
2728
};
2829

2930
if ispc
30-
p_parent{12}{2} = "c:/";
31-
p_parent{13}{2} = "c:/";
32-
p_parent{14}{2} = "c:/a";
33-
p_parent{15}{2} = "c:/a";
34-
p_parent{end+1} = {"c:/a", "c:/"};
35-
p_parent{end+1} = {"c:", "c:/"};
31+
p{12}{2} = "c:/";
32+
p{13}{2} = "c:/";
33+
p{14}{2} = "c:/a";
34+
p{15}{2} = "c:/a";
35+
p{end+1} = {"c:/a", "c:/"};
36+
p{end+1} = {"c:", "c:/"};
3637

3738
end
3839

@@ -41,8 +42,8 @@
4142

4243

4344
methods (Test)
44-
function test_parent(tc, p_parent)
45-
tc.verifyEqual(stdlib.parent(p_parent{1}), p_parent{2}, p_parent{1})
45+
function test_parent(tc, p, use_java)
46+
tc.verifyEqual(stdlib.parent(p{1}, use_java), p{2}, p{1})
4647
end
4748
end
4849

0 commit comments

Comments
 (0)