Skip to content

Commit 3c3bc02

Browse files
committed
is_exe: default use Java true
1 parent c57eb22 commit 3c3bc02

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

+stdlib/is_exe.m

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,30 @@
66
function ok = is_exe(p, use_java)
77
arguments
88
p (1,1) string
9-
use_java (1,1) logical = false
9+
use_java (1,1) logical = true
1010
end
1111

1212
if ~isfile(p)
1313
ok = false;
1414
return
1515
end
1616

17-
if stdlib.isoctave()
18-
ok = javaObject("java.io.File", p).canExecute();
19-
elseif use_java
17+
if use_java
2018
% about the same time as fileattrib
21-
% doesn't need absolute path like other Java functions
2219
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/io/File.html#canExecute()
23-
ok = java.io.File(p).canExecute();
24-
2520
% more complicated
2621
% ok = java.nio.file.Files.isExecutable(java.io.File(stdlib.canonical(p)).toPath());
2722

23+
try
24+
ok = java.io.File(p).canExecute();
25+
catch e
26+
if strcmp(e.identifier, "Octave:undefined-function")
27+
ok = javaObject("java.io.File", p).canExecute();
28+
else
29+
rethrow(e);
30+
end
31+
end
32+
2833
else
2934

3035
if ~strlength(p)

example/bench_is_exe.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_exe(f, false);
9+
fjava = @() stdlib.is_exe(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/TestWhich.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ function test_which_name(tc)
1616

1717
tc.verifyEmpty(stdlib.which(tempname))
1818

19+
u = stdlib.has_java();
20+
1921
if ispc
2022
n = "pwsh.exe";
2123
else
@@ -35,8 +37,8 @@ function test_is_exe_which_fullpath(tc)
3537
import matlab.unittest.constraints.IsFile
3638
import matlab.unittest.constraints.EndsWithSubstring
3739

38-
tc.verifyFalse(stdlib.is_exe(""))
39-
tc.verifyFalse(stdlib.is_exe(tempname))
40+
tc.verifyFalse(stdlib.is_exe("", u))
41+
tc.verifyFalse(stdlib.is_exe(tempname, u))
4042

4143
n = "matlab";
4244
%% is_exe test
@@ -46,7 +48,7 @@ function test_is_exe_which_fullpath(tc)
4648
else
4749
fp = p;
4850
end
49-
tc.verifyTrue(stdlib.is_exe(fp))
51+
tc.verifyTrue(stdlib.is_exe(fp, u))
5052
%% which: test absolute path
5153
exe = stdlib.which(p);
5254

0 commit comments

Comments
 (0)