Skip to content

Commit c958cbf

Browse files
committed
samepath: speedup
1 parent b09e26c commit c958cbf

File tree

6 files changed

+67
-44
lines changed

6 files changed

+67
-44
lines changed

+stdlib/+java/samepath.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
function y = samepath(path1, path2)
44

5-
f1 = java.io.File(path1);
6-
f2 = java.io.File(path2);
5+
try
6+
f1 = java.io.File(path1);
7+
f2 = java.io.File(path2);
78

8-
if f1.exists() && f2.exists()
9-
y = java.nio.file.Files.isSameFile(javaAbsolutePath(f1), javaAbsolutePath(f2));
10-
else
11-
y = false;
9+
if f1.exists() && f2.exists()
10+
y = java.nio.file.Files.isSameFile(javaAbsolutePath(f1), javaAbsolutePath(f2));
11+
else
12+
y = false;
13+
end
14+
catch e
15+
javaException(e)
16+
y = logical.empty;
1217
end
1318

1419
end

+stdlib/+perl/samepath.m

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
r = logical.empty;
44

5-
exe = stdlib.perl_exe();
6-
if stdlib.strempty(exe)
7-
return
8-
end
5+
try %#ok<TRYNC>
6+
exe = stdlib.perl_exe();
7+
if stdlib.strempty(exe)
8+
return
9+
end
910

10-
c = stdlib.perl.perl2cmd('(@ARGV==2) or exit 1; @s1 = stat shift or exit 1; @s2 = stat shift or exit 1; exit(($s1[0]==$s2[0] && $s1[1]==$s2[1]) ? 0 : 1)');
11+
c = stdlib.perl.perl2cmd('(@ARGV==2) or exit 1; @s1 = stat shift or exit 1; @s2 = stat shift or exit 1; exit(($s1[0]==$s2[0] && $s1[1]==$s2[1]) ? 0 : 1)');
1112

12-
cmd = sprintf('"%s" -e %s "%s" "%s"', exe, c, file1, file2);
13+
cmd = sprintf('"%s" -e %s "%s" "%s"', exe, c, file1, file2);
1314

14-
s = system(cmd);
15+
s = system(cmd);
1516

16-
r = s == 0;
17+
r = s == 0;
18+
end
1719

1820
end

+stdlib/samepath.m

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,38 @@
1313
% * path1, path2: paths to compare
1414
% * backend: backend to use
1515
%%% Outputs
16-
% * ok: true if paths are the same
16+
% * i: true if paths are the same
1717
% * b: backend used
1818

19-
function [ok, b] = samepath(path1, path2, backend)
19+
function [i, b] = samepath(path1, path2, backend)
2020
arguments
2121
path1 string
2222
path2 string
2323
backend (1,:) string = ["python", "java", "perl", "sys", "native"]
2424
end
2525

26-
o = stdlib.Backend(mfilename(), backend);
27-
b = o.backend;
26+
i = logical.empty;
2827

29-
if (isscalar(path1) && isscalar(path2)) || b == "native"
30-
ok = o.func(path1, path2);
31-
else
32-
ok = arrayfun(o.func, path1, path2);
33-
end
28+
for b = backend
29+
switch b
30+
case "java"
31+
i = stdlib.java.samepath(path1, path2);
32+
case "native"
33+
i = stdlib.native.samepath(path1, path2);
34+
case "perl"
35+
i = stdlib.perl.samepath(path1, path2);
36+
case "python"
37+
if stdlib.matlabOlderThan('R2022a'), continue, end
38+
i = stdlib.python.samepath(path1, path2);
39+
case "sys"
40+
i = stdlib.sys.samepath(path1, path2);
41+
otherwise
42+
error("stdlib:hard_link_count:ValueError", "Unknown backend: %s", b)
43+
end
3444

45+
if ~isempty(i)
46+
return
47+
end
48+
end
3549

3650
end

example/BenchmarkSamepath.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@
2222
methods (Test)
2323

2424
function bench_exist(tc, backend)
25-
tc.startMeasuring()
26-
y = tc.fun(tc.exist(1), tc.exist(2), backend);
27-
tc.stopMeasuring()
25+
while tc.keepMeasuring()
26+
y = tc.fun(tc.exist(1), tc.exist(2), backend);
27+
end
28+
29+
tc.verifyEqual(y, true)
30+
end
31+
32+
function bench_main(tc)
33+
while tc.keepMeasuring()
34+
y = tc.fun(tc.exist(1), tc.exist(2));
35+
end
2836

2937
tc.verifyEqual(y, true)
3038
end

example/BenchmarkSamepathRun.m

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
function [r, s] = BenchmarkSamepathRun()
1+
function BenchmarkSamepathRun()
22
tname = "BenchmarkSamepath";
33

44
%% Exist, same
55
r.same = run_bench(tname + "/bench_exist");
6-
s.exist = sampleSummary(r.same);
7-
disp(sortrows(s.exist, "Median"))
6+
s.exist = sortrows(sampleSummary(r.same), "Median");
7+
disp(s.exist(:, ["Name", "SampleSize", "Mean", "Median"]))
8+
%% Main
9+
r.main = run_bench(tname + "/bench_main");
10+
s.main = sortrows(sampleSummary(r.main), "Median");
11+
disp(s.main(:, ["Name", "SampleSize", "Mean", "Median"]))
812
%% Exist, different
913
r.diff = run_bench(tname + "/bench_diff");
10-
s.diff = sampleSummary(r.diff);
11-
disp(sortrows(s.diff, "Median"))
14+
s.diff = sortrows(sampleSummary(r.diff), "Median");
15+
disp(s.diff(:, ["Name", "SampleSize", "Mean", "Median"]))
1216
%% Not Exist
1317
r.not = run_bench(tname + "/bench_not_exist");
14-
s.not = sampleSummary(r.not);
15-
disp(sortrows(s.not, "Median"))
18+
s.not = sortrows(sampleSummary(r.not), "Median");
19+
disp(s.not(:, ["Name", "SampleSize", "Mean", "Median"]))
1620

1721
end
1822

1923

2024
function result = run_bench(name)
2125
suite = testsuite(name);
22-
exp = matlab.perftest.TimeExperiment.limitingSamplingError(MaxSamples=20, RelativeMarginOfError=0.1);
26+
exp = matlab.perftest.TimeExperiment.limitingSamplingError(RelativeMarginOfError=0.1);
2327
result = exp.run(suite);
2428
end

test/TestSame.m

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,7 @@ function test_samepath_notexist(tc, backend)
3333
tc.verifyFalse(stdlib.samepath("", "", backend))
3434
tc.verifyFalse(stdlib.samepath(t, t, backend))
3535
end
36-
end
37-
3836

39-
methods (Test, TestTags={'R2024a'})
40-
function test_samepath_array(tc, backend)
41-
in = [string(mfilename), string(mfilename('fullpath'))] + ".m";
42-
r = stdlib.samepath(in, fliplr(in), backend);
43-
tc.verifyClass(r, 'logical')
44-
tc.verifyEqual(r, [true, true])
4537
end
46-
end
47-
4838

4939
end

0 commit comments

Comments
 (0)