我有一个代码迭代超过100 000个文件,并获得他们的相对路径到一些根目录和代码工作,但比其他丑陋的解决方案(它是奇怪的代码,但它的速度更快)相对较慢。java文件相对方法性能
原始代码是在这里:
File file, URI rootDirURI
for() {
blabla = rootDirURI.relativize(file.toURI()).getPath()
}
VS
File file, URI rootDirURI
for() {
String rootDirPath = rootDirURI.getPath().substring(1); // cut the first slash
rootDirPath = rootDirPath.replaceAll("/", "\\\\"); // correct windows slashes
String finalPath = file.getAbsolutePath().replace(rootDirPath, ""); // clear the root path: relativize
blabla = finalPath.replace("\\", "/"); // slashes
}
好了第一个for循环运行长则2分钟和不到2秒,第二个跑......文件通过UNC路径加载,但是这个for循环是在Files.walkFileTree被执行之后。我在我的文件系统中创建了符号链接,其目标为UNC路径,如\\ 192.168.1.x \ public \第一部分加载ArrayList中的所有内容,第二部分将某些操作(上面的代码for循环)应用于该ArrayList中的文件。
这是否意味着相对性能差或toURI方法?