Skip to content

Commit 2b81baa

Browse files
committed
relative_to: native only as it's 5x faster than python
1 parent f61d20f commit 2b81baa

File tree

8 files changed

+97
-74
lines changed

8 files changed

+97
-74
lines changed

+stdlib/+native/relative_to.m

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

+stdlib/+python/relative_to.m

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

+stdlib/proximate_to.m

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
%%% Inputs
44
% * base: directory to which the other path should be relative
55
% * other: path to be made relative
6-
% * backend: backend to use
76
%%% Outputs
87
% * rel: relative path from base to other
9-
% * b: backend used
108

11-
function [rel, b] = proximate_to(base, other, backend)
9+
function rel = proximate_to(base, other)
1210
arguments
1311
base (1,1) string
1412
other (1,1) string
15-
backend (1,:) string = ["python", "native"]
1613
end
1714

18-
[rel, b] = stdlib.relative_to(base, other, backend);
15+
rel = stdlib.relative_to(base, other);
1916
if stdlib.strempty(rel)
2017
rel = other;
2118
end

+stdlib/relative_to.m

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,44 @@
1-
%% RELATIVE_TO relative path to base
1+
%% RELATIVE_TO relative path to target from base
22
%
33
%%% Inputs
44
% * base: directory to which the other path should be relative
5-
% * other: path to be made relative
6-
% * backend: backend to use
5+
% * target: path to be made relative
76
%%% Outputs
8-
% * rel: relative path from base to other
9-
% * b: backend used
7+
% * rel: relative path from base to target
108
%
119
% Note: Java Path.relativize has an algorithm so different that we choose not to use it.
12-
% javaPathObject(base).relativize(javaPathObject(other))
10+
% javaPathObject(base).relativize(javaPathObject(target))
1311
% https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html#relativize-java.nio.file.Path-
1412

15-
function [rel, b] = relative_to(base, other, backend)
13+
function rel = relative_to(base, target)
1614
arguments
1715
base (1,1) string
18-
other (1,1) string
19-
backend (1,:) string = ["native", "python"]
16+
target (1,1) string
2017
end
2118

22-
o = stdlib.Backend(mfilename(), backend);
23-
rel = o.func(base, other);
24-
b = o.backend;
19+
% matlab.io.internal.filesystem.relativepath only works on Windows (!) and only
20+
% then with / filesep.
21+
22+
if stdlib.strempty(base) || stdlib.strempty(target)
23+
rel = "";
24+
return
25+
end
26+
27+
fs = ["/", filesep];
28+
29+
tp = split(fullfile(target), fs);
30+
bp = split(fullfile(base), fs);
31+
32+
% Find the common base portion
33+
n = 0;
34+
while n < length(tp) && n < length(bp) && strcmp(tp{n+1}, bp{n+1})
35+
n = n + 1;
36+
end
37+
38+
rel = join(tp(n+1:end), filesep);
39+
40+
if stdlib.strempty(rel)
41+
rel = ".";
42+
end
2543

2644
end

example/+python/relative_to.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function rel = relative_to(base, target)
2+
arguments
3+
base (1,1) string
4+
target (1,1) string
5+
end
6+
7+
rel = "";
8+
9+
if stdlib.strempty(base) || stdlib.strempty(target)
10+
return
11+
end
12+
13+
try
14+
rel = string(py.str(py.pathlib.Path(target).relative_to(base)));
15+
catch e
16+
pythonException(e)
17+
end
18+
19+
end

example/BenchmarkRelativeTo.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
classdef (SharedTestFixtures={ matlab.unittest.fixtures.PathFixture(fileparts(fileparts(mfilename('fullpath'))))}) ...
2+
BenchmarkRelativeTo < matlab.perftest.TestCase
3+
4+
properties
5+
in = [pwd(), pwd() + "/a"]
6+
ref = "a"
7+
end
8+
9+
properties (TestParameter)
10+
fun = {@stdlib.relative_to, @stdlib.python.relative_to}
11+
end
12+
13+
14+
methods (Test)
15+
16+
function bench_main(tc, fun)
17+
while tc.keepMeasuring()
18+
r = fun(tc.in(1), tc.in(2));
19+
end
20+
21+
tc.verifyEqual(r, tc.ref)
22+
end
23+
24+
end
25+
26+
end

example/BenchmarkRelativeToRun.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function BenchmarkRelativeToRun()
2+
tname = "BenchmarkRelativeTo";
3+
4+
%% Exist, same
5+
r.same = run_bench(tname + "/bench_main");
6+
s.exist = sortrows(sampleSummary(r.same), "Median");
7+
disp(s.exist(:, ["Name", "SampleSize", "Mean", "Median"]))
8+
9+
end
10+
11+
12+
function result = run_bench(name)
13+
suite = testsuite(name);
14+
exp = matlab.perftest.TimeExperiment.limitingSamplingError(RelativeMarginOfError=0.1);
15+
result = exp.run(suite);
16+
end

test/TestRelative.m

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

66
properties (TestParameter)
77
pr
8-
backend
98
end
109

1110

@@ -57,23 +56,19 @@
5756
}];
5857
end
5958
end
60-
61-
function backend = setupBackends()
62-
backend = init_backend("relative_to");
63-
end
6459
end
6560

6661

6762
methods (Test)
6863

69-
function test_relative_to(tc, pr, backend)
70-
r = stdlib.relative_to(pr{1}, pr{2}, backend);
64+
function test_relative_to(tc, pr)
65+
r = stdlib.relative_to(pr{1}, pr{2});
7166

7267
tc.verifyEqual(r, pr{3}, "relative_to(" + pr{1} + "," + pr{2}+")")
7368
end
7469

75-
function test_proximate_to(tc, pr, backend)
76-
r = stdlib.proximate_to(pr{1}, pr{2}, backend);
70+
function test_proximate_to(tc, pr)
71+
r = stdlib.proximate_to(pr{1}, pr{2});
7772

7873
tc.verifyEqual(r, pr{3}, "proximate_to(" + pr{1} + ", " + pr{2}+")")
7974
end

0 commit comments

Comments
 (0)