File tree Expand file tree Collapse file tree 6 files changed +27
-8
lines changed Expand file tree Collapse file tree 6 files changed +27
-8
lines changed Original file line number Diff line number Diff line change 3131 return
3232end
3333
34- e = stdlib .exists(c );
34+ e = stdlib .exists(c , use_java );
3535
3636if ~stdlib .is_absolute(c )
3737 if e
Original file line number Diff line number Diff line change 1919% ok = java.nio.file.Files.createSymbolicLink(java.io.File(link).toPath(), java.io.File(target).toPath());
2020% the above should work, but Matlab Java doesn't recognize the optional argument omitted.
2121
22- if stdlib .exists(link )
22+ if stdlib .exists(link , stdlib .has_java() )
2323 ok = false ;
2424 return
2525end
Original file line number Diff line number Diff line change 55% %% Outputs
66% * ok: true if exists
77%
8- % Ref: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#exists()
98
109function ok = exists(p , use_java )
1110arguments
1211 p (1 ,1 ) string
13- use_java (1 ,1 ) logical = false
12+ use_java (1 ,1 ) logical = true
1413end
1514
1615if use_java
17- % Java takes 2x to 10x as long as intrinsic way worst case
18- % the intrinsic way above is at least not slower
1916
17+ % https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#exists(java.nio.file.Path,java.nio.file.LinkOption...)
18+ % this takes 2x longer than java.io.File.exists()
19+ % opt = java.nio.file.LinkOption.values;
20+ % ok = java.nio.file.Files.exists(java.io.File(p).toPath(), opt);
21+
22+ % https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#exists()
23+ % about the same speed as native Matlab isfile || isfolder
2024 ok = java .io .File(p ).exists();
2125else
2226 ok = isfile(p ) || isfolder(p );
Original file line number Diff line number Diff line change 1717end
1818
1919issame = false ;
20- if ~stdlib .exists(path1 ) || ~stdlib .exists(path2 )
20+ if ~stdlib .exists(path1 , use_java ) || ~stdlib .exists(path2 , use_java )
2121 return
2222end
2323
Original file line number Diff line number Diff line change 1+ %% benchmark for exists()
2+
3+ f = mfilename(" fullpath" );
4+ % f = tempname;
5+
6+ fno = @() stdlib .exists(f , false );
7+ fjava = @() stdlib .exists(f , true );
8+
9+ t_no = timeit(fno );
10+ t_java = timeit(fjava );
11+
12+ disp(" No Java: " + t_no + " s" )
13+ disp(" Java: " + t_java + " s" )
14+
15+ disp(" Java is " + t_no / t_java + " times faster" )
Original file line number Diff line number Diff line change @@ -52,7 +52,7 @@ function setup_path(tc)
5252methods (Test , ParameterCombination = ' sequential' )
5353
5454function test_exists(tc , p_exists )
55- tc .verifyEqual(stdlib .exists(p_exists{1 }), p_exists{2 })
55+ tc .verifyEqual(stdlib .exists(p_exists{1 }, stdlib .has_java() ), p_exists{2 })
5656end
5757
5858
You can’t perform that action at this time.
0 commit comments