Skip to content

Commit db3125f

Browse files
committed
absolute: improve efficiency
1 parent 972de32 commit db3125f

File tree

7 files changed

+84
-81
lines changed

7 files changed

+84
-81
lines changed

+stdlib/absolute.m

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,69 +17,50 @@
1717
% non-existant path is made absolute relative to pwd
1818

1919
function c = absolute(p, base, expand_tilde, use_java)
20-
% arguments
21-
% p (1,1) string
22-
% base (1,1) string = ""
23-
% expand_tilde (1,1) logical = true
24-
% use_java (1,1) logical = false
25-
% end
26-
if nargin < 2, base = ""; end
27-
if nargin < 3, expand_tilde = true; end
28-
if nargin < 4, use_java = false; end
29-
30-
cwd = pwd();
31-
32-
try %#ok<TRYNC>
33-
cwd = string(cwd);
34-
p = string(p);
35-
base = string(base);
20+
arguments
21+
p (1,1) string
22+
base (1,1) string = ""
23+
expand_tilde (1,1) logical = true
24+
use_java (1,1) logical = true
3625
end
3726

38-
cwd = stdlib.posix(cwd);
39-
40-
Lb = stdlib.len(base);
27+
if expand_tilde
28+
c = stdlib.expanduser(p, use_java);
29+
else
30+
c = stdlib.posix(p);
31+
end
4132

42-
if stdlib.len(p) == 0 && Lb == 0
43-
c = cwd;
44-
return
33+
if stdlib.is_absolute(c)
34+
return
4535
end
4636

47-
if expand_tilde
48-
c = stdlib.expanduser(p, use_java);
37+
if stdlib.len(base) == 0
38+
b = pwd();
39+
elseif expand_tilde
40+
b = stdlib.expanduser(base, use_java);
4941
else
50-
c = p;
42+
b = base;
5143
end
44+
b = stdlib.posix(b);
5245

53-
if stdlib.is_absolute(c, use_java)
54-
c = stdlib.posix(c);
46+
if stdlib.is_absolute(b)
47+
if ischar(c)
48+
c = strcat(b, '/', c);
49+
else
50+
c = b + "/" + c;
51+
end
5552
else
56-
% .getAbsolutePath(), .toAbsolutePath()
57-
% default is Documents/Matlab, which is probably not wanted.
58-
if Lb == 0
59-
if ischar(cwd)
60-
c = strcat(cwd, '/', c);
61-
else
62-
c = cwd + "/" + c;
63-
end
53+
if ischar(c)
54+
c = strcat(pwd(), '/', b, '/', c);
6455
else
65-
d = stdlib.absolute(base, "", expand_tilde, use_java);
66-
if isempty(c) || stdlib.len(c) == 0
67-
c = d;
68-
else
69-
if ischar(d)
70-
c = strcat(d, '/', c);
71-
else
72-
c = d + "/" + c;
73-
end
74-
end
56+
c = pwd() + "/" + b + "/" + c;
7557
end
7658
end
7759

78-
% not needed:
79-
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getAbsolutePath()
60+
c = stdlib.drop_slash(c);
8061

8162
end
8263

8364

84-
%!assert(absolute('', ''), posix(pwd))
85-
%!assert(absolute('a/b', ''), posix(fullfile(pwd, 'a/b')))
65+
%!assert(absolute('', '', false), posix(pwd))
66+
%!assert(absolute('a/b', '', false), posix(strcat(pwd(), '/a/b')))

+stdlib/is_regular_file.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
%% IS_REGULAR_FILE check if path is a regular file
22

33
function r = is_regular_file(p)
4-
% arguments
5-
% p (1,1) string
6-
% end
4+
arguments
5+
p (1,1) string
6+
end
77

88
% needs absolute()
99
p = stdlib.absolute(p, "", false, true);

+stdlib/posix.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
%
55

66
function r = posix(p)
7-
% arguments
8-
% p (1,1) string
9-
% end
7+
arguments
8+
p (1,1) string
9+
end
1010

1111
if ispc
1212
r = strrep(p, '\', '/');

example/bench_absolute.m

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

example/bench_is_readable.m

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

test/TestFilePure.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
p_is_absolute
66

7-
87
p_filename = {
98
{"", ""}, ...
109
{"/a/b/c", "c"}, ...

test/TestResolve.m

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,24 @@ function setup_path(tc)
77
tc.applyFixture(matlab.unittest.fixtures.PathFixture(top))
88
end
99

10+
11+
function setup_workdir(tc)
12+
import matlab.unittest.constraints.IsFile
13+
import matlab.unittest.fixtures.TemporaryFolderFixture
14+
import matlab.unittest.fixtures.CurrentFolderFixture
15+
16+
workdir = tc.applyFixture(TemporaryFolderFixture).Folder;
17+
tc.applyFixture(CurrentFolderFixture(workdir))
18+
end
19+
1020
end
1121

1222

1323
methods(Test)
1424

1525
function test_absolute(tc)
16-
import matlab.unittest.fixtures.TemporaryFolderFixture
17-
import matlab.unittest.fixtures.CurrentFolderFixture
18-
import matlab.unittest.constraints.StartsWithSubstring
19-
import matlab.unittest.constraints.EndsWithSubstring
2026

21-
td = tc.applyFixture(TemporaryFolderFixture).Folder;
22-
tc.applyFixture(CurrentFolderFixture(td))
23-
24-
td = stdlib.posix(string(td));
27+
td = stdlib.posix(string(pwd()));
2528

2629
tc.verifyEqual(stdlib.absolute(""), td)
2730
tc.verifyEqual(stdlib.absolute("",""), td)
@@ -40,16 +43,10 @@ function test_absolute(tc)
4043

4144

4245
function test_resolve_non_exist(tc)
43-
import matlab.unittest.fixtures.TemporaryFolderFixture
44-
import matlab.unittest.fixtures.CurrentFolderFixture
4546
import matlab.unittest.constraints.StartsWithSubstring
46-
import matlab.unittest.constraints.EndsWithSubstring
4747
import matlab.unittest.constraints.ContainsSubstring
4848

49-
td = stdlib.posix(tc.applyFixture(TemporaryFolderFixture).Folder);
50-
tc.applyFixture(CurrentFolderFixture(td))
51-
52-
td = string(td);
49+
td = stdlib.posix(string(pwd()));
5350

5451
% all non-existing files
5552

@@ -79,22 +76,14 @@ function test_resolve_non_exist(tc)
7976
end
8077

8178
function test_resolve_exist(tc)
82-
import matlab.unittest.fixtures.TemporaryFolderFixture
83-
import matlab.unittest.fixtures.CurrentFolderFixture
84-
import matlab.unittest.constraints.StartsWithSubstring
85-
import matlab.unittest.constraints.EndsWithSubstring
86-
import matlab.unittest.constraints.ContainsSubstring
87-
88-
td = stdlib.posix(tc.applyFixture(TemporaryFolderFixture).Folder);
89-
tc.applyFixture(CurrentFolderFixture(td))
9079

91-
td = string(td);
80+
td = stdlib.posix(string(pwd()));
9281

9382
r = stdlib.parent(mfilename('fullpath'));
9483
rp = stdlib.parent(r);
9584
tc.verifyEqual(stdlib.resolve(rp), string(stdlib.parent(r)))
9685

97-
h = stdlib.homedir;
86+
h = stdlib.homedir();
9887
tc.verifyEqual(stdlib.resolve("~"), h)
9988
tc.verifyEqual(stdlib.resolve("~/"), h)
10089
tc.verifyEqual(stdlib.resolve("~/.."), stdlib.parent(h))

0 commit comments

Comments
 (0)