Skip to content

Commit 8fe4bd0

Browse files
committed
samepath: speed optimize
1 parent 102ff18 commit 8fe4bd0

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

+stdlib/+python/samepath.m

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
function y = samepath(path1, path2)
2-
arguments
3-
path1 (1,1) string
4-
path2 (1,1) string
5-
end
62

73
try
84
y = py.os.path.samefile(path1, path2);
95
catch e
10-
if contains(e.message, "FileNotFoundError")
11-
y = false;
12-
else
13-
rethrow(e);
14-
end
6+
pythonException(e)
7+
y = false;
158
end
169

1710
end

+stdlib/samepath.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
arguments
2121
path1 string
2222
path2 string
23-
backend (1,:) string = ["python", "perl", "java", "sys", "native"]
23+
backend (1,:) string = ["python", "java", "perl", "sys", "native"]
2424
end
2525

26-
% For this function, Python is over 10x faster than Java
27-
2826
o = stdlib.Backend(mfilename(), backend);
2927
b = o.backend;
3028

example/BenchmarkSamepath.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
classdef (SharedTestFixtures={ matlab.unittest.fixtures.PathFixture("..")}) ...
22
BenchmarkSamepath < matlab.perftest.TestCase
33

4+
properties
5+
exist = [".", pwd()]
6+
diff = [".", mfilename('fullpath') + ".m"]
7+
not = tempname()
8+
fun = @stdlib.samepath
9+
end
10+
411
properties(TestParameter)
512
backend
613
end
@@ -16,7 +23,7 @@
1623

1724
function bench_exist(tc, backend)
1825
tc.startMeasuring()
19-
y = stdlib.samepath('.', pwd(), backend);
26+
y = tc.fun(tc.exist(1), tc.exist(2), backend);
2027
tc.stopMeasuring()
2128

2229
tc.verifyEqual(y, true)
@@ -25,7 +32,7 @@ function bench_exist(tc, backend)
2532

2633
function bench_diff(tc, backend)
2734
tc.startMeasuring()
28-
y = stdlib.samepath('.', mfilename('fullpath'), backend);
35+
y = tc.fun(tc.diff(1), tc.diff(2), backend);
2936
tc.stopMeasuring()
3037

3138
tc.verifyEqual(y, false)
@@ -34,7 +41,7 @@ function bench_diff(tc, backend)
3441

3542
function bench_not_exist(tc, backend)
3643
tc.startMeasuring()
37-
y = stdlib.samepath('not-exist', 'not-exist', backend);
44+
y = tc.fun(tc.not, tc.not, backend);
3845
tc.stopMeasuring()
3946

4047
tc.verifyEqual(y, false)

example/BenchmarkSamepathRun.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
function [r, s] = BenchmarkSamepathRun()
2+
tname = "BenchmarkSamepath";
3+
24
%% Exist, same
3-
r.same = run_bench('BenchmarkSamepath/bench_exist');
5+
r.same = run_bench(tname + "/bench_exist");
46
s.exist = sampleSummary(r.same);
57
disp(sortrows(s.exist, "Median"))
68
%% Exist, different
7-
r.diff = run_bench('BenchmarkSamepath/bench_diff');
9+
r.diff = run_bench(tname + "/bench_diff");
810
s.diff = sampleSummary(r.diff);
911
disp(sortrows(s.diff, "Median"))
1012
%% Not Exist
11-
r.not = run_bench('BenchmarkSamepath/bench_not_exist');
13+
r.not = run_bench(tname + "/bench_not_exist");
1214
s.not = sampleSummary(r.not);
1315
disp(sortrows(s.not, "Median"))
1416

@@ -17,6 +19,6 @@
1719

1820
function result = run_bench(name)
1921
suite = testsuite(name);
20-
exp = matlab.perftest.TimeExperiment.limitingSamplingError(MaxSamples=25, RelativeMarginOfError=0.1);
22+
exp = matlab.perftest.TimeExperiment.limitingSamplingError(MaxSamples=20, RelativeMarginOfError=0.1);
2123
result = exp.run(suite);
2224
end

0 commit comments

Comments
 (0)