我一直在玩各种方法,似乎使用File []似乎工作得最快(下面的代码)。我编译并从终端运行。
测试方法是每个文件路径上10次传递的平均值,有4个文件路径。文件路径1有1470个文件,文件路径2有1435个文件,文件路径3和4都有7个文件。
按方法分解结果,然后按子方法分解。第一个数字是两者都需要的总时间,第二个数字是用于操作的总内存。
文件数组
J8流方法#1
J8流方法#2
。
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.stream.Stream;
public class Testing {
public static void main(String[] args) {
for (int loopedTimes = 0 ; loopedTimes < 10 ; loopedTimes++) {
for (int testCase = 0; testCase < 6; testCase++) {
int argumentNumber = 0;
for (String passedArguments: args) {
int intM = 0;
int postM = 0;
LinkedHashMap<Integer, File> fileArrayTest = null;
LinkedHashMap<Integer, Path> java8StreamForEachTest = null;
LinkedHashMap<Integer, Path> java8StreamIterationTest = null;
StringBuffer sb = null;
int[] method = null;
int[] test = null;
String[] identifier = null;
System.gc();
intM = usage();
fileArrayTest = new LinkedHashMap<Integer, File>();
java8StreamForEachTest = new LinkedHashMap<Integer, Path>();
java8StreamIterationTest = new LinkedHashMap<Integer, Path>();
sb = new StringBuffer();
method = new int[2];
test = new int[2];
identifier = new String[2];
switch (testCase) {
case 0:
method = java8StreamForEachInput(passedArguments, java8StreamForEachTest);
test = forEachTest(testCase, argumentNumber, loopedTimes, java8StreamForEachTest);
identifier = new String[]{"For Each","For Each"};
break;
case 1:
method = java8StreamForEachInput(passedArguments, java8StreamForEachTest);
test = iteratorTest(testCase, argumentNumber, loopedTimes, java8StreamForEachTest);
identifier = new String[]{"For Each","Iterator"};
break;
case 2:
method = java8StreamIteratedInput(passedArguments, java8StreamIterationTest);
test = forEachTest(testCase, argumentNumber, loopedTimes, java8StreamIterationTest);
identifier = new String[]{"Iterator","For Each"};
break;
case 3:
method = java8StreamIteratedInput(passedArguments, java8StreamIterationTest);
test = iteratorTest(testCase, argumentNumber, loopedTimes, java8StreamIterationTest);
identifier = new String[]{"Iterator","Iterator"};
break;
case 4:
method = fileArrayInput(passedArguments, fileArrayTest);
test = forEachTest(testCase, argumentNumber, loopedTimes, fileArrayTest);
identifier = new String[]{"File Array","For Each"};
break;
case 5:
method = fileArrayInput(passedArguments, fileArrayTest);
test = iteratorTest(testCase, argumentNumber, loopedTimes, fileArrayTest);
identifier = new String[]{"File Array","Iterator"};
break;
default:
System.exit(0);
break;
}
postM = usage();
// Spit out results
sb
.append("loopedTimes | ")
.append(loopedTimes)
.append(" | argumentNumber | ")
.append(argumentNumber)
.append(" | testCase | ")
.append(testCase)
.append(" | ")
.append(identifier[0])
.append(" | ")
.append(passedArguments)
.append(" | Processing Time | ")
.append(method[1])
.append(" | ")
.append(identifier[1])
.append(" | Insertion | ")
.append(test[1])
.append(" | Elements | ")
.append(test[0])
.append(" | Total | ")
.append(test[1] + method[1])
.append(" | intM | ")
.append(intM)
.append(" | postM | ")
.append(postM)
.append("\n");
try (PrintWriter writer = new PrintWriter("results/_" + loopedTimes + "-" + argumentNumber + "-" + testCase + ".txt", "UTF-8")) {
writer.print(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
argumentNumber++;
}
}
}
}
static int[] fileArrayInput(String passedArguments, LinkedHashMap<Integer, File> fileArrayTest){
long startFileArrayTime = System.nanoTime();
try {
File[] listOfFiles = new File(passedArguments).listFiles();
for (File fileName: listOfFiles) {
fileArrayTest.put(fileName.hashCode(), fileName);
}
} catch (Exception e) {
e.printStackTrace();
}
long endFileArrayTime = System.nanoTime() - startFileArrayTime;
return new int[]{ 0, (int)endFileArrayTime};
}
static int[] java8StreamForEachInput(String passedArguments, LinkedHashMap<Integer, Path> java8StreamForEachTest){
long startJava8StreamForEachTime = System.nanoTime();
try(Stream<Path> paths = Files.walk(Paths.get(passedArguments))) {
for (Path filePath : new Iterable<Path>() {
@Override
public Iterator<Path> iterator() {
return paths.iterator();
}
}) {
java8StreamForEachTest.put(filePath.hashCode(), filePath);
}
} catch (IOException e) {
e.printStackTrace();
}
long endJava8StreamForEachTime = System.nanoTime() - startJava8StreamForEachTime;
return new int[]{ 0, (int)endJava8StreamForEachTime};
}
static int[] java8StreamIteratedInput(String passedArguments, LinkedHashMap<Integer, Path> java8StreamIterationTest){
long startJava8IterationTime = System.nanoTime();
try(Stream<Path> paths = Files.walk(Paths.get(passedArguments))) {
paths.forEach(filePath -> {
java8StreamIterationTest.put(filePath.hashCode(), filePath);
});
} catch (IOException e) {
e.printStackTrace();
}
long endJava8StreamIterationTime = System.nanoTime() - startJava8IterationTime;
return new int[]{ 0, (int)endJava8StreamIterationTime};
}
static int[] forEachTest(int testCase, int argumentNumber, int loopedTimes, LinkedHashMap<Integer,?> passedLinkedHashMap) {
int forEachElements = 0;
long startForEachTime = System.nanoTime();
try (PrintWriter writer = new PrintWriter("data/forEachElements1_arg-" + loopedTimes + "-" + argumentNumber + "-" + testCase + ".txt", "UTF-8")) {
for (Entry<Integer, ?> entry : passedLinkedHashMap.entrySet()) {
writer.println(entry.getValue().toString());
forEachElements++;
}
} catch (IOException e) {
e.printStackTrace();
}
long endForEachTime = System.nanoTime() - startForEachTime;
return new int[]{forEachElements, (int)endForEachTime};
}
static int[] iteratorTest(int testCase, int argumentNumber, int loopedTimes, LinkedHashMap<Integer,?> passedLinkedHashMap) {
System.gc();
// Iteration - 1
int iterationElements = 0;
long startIterationTime = System.nanoTime();
Collection<?> c1 = passedLinkedHashMap.values();
Iterator<?> itr1 = c1.iterator();
try (PrintWriter writer = new PrintWriter("data/iterationElements1_arg-" + loopedTimes + "-" + argumentNumber + "-" + testCase + ".txt", "UTF-8")) {
while(itr1.hasNext()) {
writer.println(itr1.next().toString());
iterationElements++;
}
} catch (IOException e) {
e.printStackTrace();
}
long endIterationTime = System.nanoTime() - startIterationTime;
return new int[]{iterationElements, (int)endIterationTime};
}
static int usage() {
Runtime runtime = Runtime.getRuntime();
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
return (int)((freeMemory + (maxMemory - allocatedMemory))/1024);
}
}
ps - 抱歉可怕的格式化
编辑
5 Result Sets