Skip to content

Commit 5a0ec07

Browse files
committed
parent: native only
The overhead and complication of multiple backends was not worth the possible 10% speedup in Python
1 parent 420145f commit 5a0ec07

File tree

8 files changed

+53
-53
lines changed

8 files changed

+53
-53
lines changed

+stdlib/+java/parent.m

Lines changed: 0 additions & 16 deletions
This file was deleted.

+stdlib/+native/parent.m

Lines changed: 0 additions & 17 deletions
This file was deleted.

+stdlib/+python/is_exe.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
function y = is_exe(p)
2+
arguments
3+
p (1,1) string
4+
end
25

36
y = isfile(p);
47

+stdlib/parent.m

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,26 @@
22
%
33
%% Inputs
44
% * file: path to file or folder
5-
% * backend: backend to use
65
%% Outputs
76
% * par: parent directory of path
8-
% * b: backend used
97
%% Examples:
108
% stdlib.parent("a/b/c") == "a/b"
119
% stdlib.parent("a/b/c/") == "a/b"
12-
%% Benchmark
13-
% Java or Python are each about 2-4x faster than native Matlab
1410

15-
function [par, b] = parent(file, backend)
11+
function p = parent(file)
1612
arguments
1713
file string
18-
backend (1,:) string = ["java", "python", "native"]
1914
end
2015

21-
[fun, b] = hbackend(backend, "parent");
16+
p = fileparts(stdlib.drop_slash(file));
2217

23-
if isscalar(file) || b == "native"
24-
par = fun(file);
25-
else
26-
par = arrayfun(fun, file);
18+
i = ~strlength(p);
19+
p(i) = ".";
20+
21+
% the ~all(i) is for Windows Matlab < R2025a
22+
if ispc() && ~all(i)
23+
i = p(~i) == stdlib.root_name(pth(~i));
24+
p(i) = strcat(p(i), filesep);
2725
end
2826

2927
end

example/+javafun/parent.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function par = parent(pth)
2+
arguments
3+
pth (1,1) string
4+
end
5+
6+
par = java.io.File(pth).getParent();
7+
if isempty(par)
8+
par = ".";
9+
if ispc()
10+
rn = stdlib.root_name(pth);
11+
if strlength(pth) && strlength(rn) && startsWith(pth, rn)
12+
par = strcat(rn, filesep);
13+
end
14+
end
15+
else
16+
par = string(par);
17+
end
18+
19+
end

+stdlib/+python/parent.m renamed to example/+python/parent.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
function par = parent(pth)
2+
arguments
3+
pth (1,1) string
4+
end
25

36
try
47
op = py.pathlib.Path(pth);

example/bench_parent.m

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
f = mfilename("fullpath") + ".m";
1+
function bench_parent()
22

3-
fno = @() stdlib.parent(f);
3+
in = mfilename("fullpath") + ".m";
4+
r = fileparts(fileparts(in));
5+
addpath(r)
6+
obj = onCleanup(@() rmpath(r));
47

5-
t_no = timeit(fno);
8+
h = @() stdlib.parent(in);
9+
fj = @() javafun.parent(in);
10+
fp = @() python.parent(in);
611

7-
disp("No Java: " + t_no + " s")
12+
tj = timeit(fj, 1) * 1e3;
13+
tp = timeit(fp, 1) * 1e3;
14+
th = timeit(h, 1) * 1e3;
15+
16+
fprintf('Native: %f\nJava: %f\nPython: %f\n', th, tj, tp);
17+
18+
end

test/TestParent.m

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
properties (TestParameter)
55
p = init_parent()
6-
backend = init_backend({'java', 'python', 'native'})
76
end
87

98
methods(TestClassSetup)
@@ -14,9 +13,9 @@ function test_dirs(tc)
1413

1514
methods (Test, TestTags=["R2019b", "pure"])
1615

17-
function test_parent(tc, p, backend)
18-
pr = stdlib.parent(p{1}, backend);
19-
tc.verifyEqual(pr, p{2}, sprintf("parent(%s, %s)", p{1}, backend))
16+
function test_parent(tc, p)
17+
pr = stdlib.parent(p{1});
18+
tc.verifyEqual(pr, p{2}, sprintf("parent(%s)", p{1}))
2019
end
2120

2221
end
@@ -30,7 +29,7 @@ function test_parent_array(tc)
3029
in = ["", ".", "..", "../..", "a/", "a/b", "ab/.parent", "ab/.parent.txt", "a/b/../.parent.txt"];
3130
exp = [".", ".", ".", "..", ".", "a", "ab", "ab", fullfile("a", "b", "..")];
3231

33-
out = stdlib.parent(in, 'native');
32+
out = stdlib.parent(in);
3433
tc.verifyEqual(out, exp)
3534
end
3635

0 commit comments

Comments
 (0)