import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class Main {
static class Result {
int size;
long listTime;
long arrayListTime;
public Result(int size) {
this.size = size;
}
}
// warm up code to make the benchmark more accurate
for (int i = 0; i < 100; i++) {
listVsArrayListLinearPerformance(1000);
}
// print the header
System.
out.
printf("%-15s %-20s %-20s%n",
"#Elements", "ArrayList", "List");
// perform the benchmarking
printResult(listVsArrayListLinearPerformance(1000));
printResult(listVsArrayListLinearPerformance(2000));
printResult(listVsArrayListLinearPerformance(3000));
printResult(listVsArrayListLinearPerformance(4000));
printResult(listVsArrayListLinearPerformance(5000));
printResult(listVsArrayListLinearPerformance(10000));
printResult(listVsArrayListLinearPerformance(20000));
}
private static void printResult(Result r) {
long min
= Math.
min(r.
listTime, r.
arrayListTime);
System.
out.
printf("%-15d %-10d (%-3.0f%%) %-10d (%-3.0f%%)%n",
r.size,
r.arrayListTime, (100 * (double) r.arrayListTime) / min,
r.listTime, (100 * (double) r.listTime) / min);
}
private static Result listVsArrayListLinearPerformance(int size) {
ArrayList
<Integer
> simpleList
= new ArrayList
<>(Arrays.
asList(-1)); ArrayList
<Integer
> arrayList
= new ArrayList
<>(Arrays.
asList(-1));
for (int i = 0; i < size; i++) {
array1
[i
] = random.
nextInt(Integer.
MAX_VALUE); array2
[i
] = random.
nextInt(Integer.
MAX_VALUE); }
Result result = new Result(size);
long before;
linearInsertionAsList(array1, simpleList);
result.
listTime = (System.
nanoTime() - before
) / 1000;
linearInsertionAsArrayList(array2, arrayList);
result.
arrayListTime = (System.
nanoTime() - before
) / 1000;
return result;
}
private static void linearInsertionAsList
(Integer[] intArray, List
<Integer
> list
) { int list_size = list.size(); // avoid calculating the size every time
for (int i = 0; i < list_size; i++) {
if (integer.compareTo(list.get(i)) >= 0) {
list.add(i, integer);
break;
}
}
}
}
private static void linearInsertionAsArrayList
(Integer[] intArray, ArrayList
<Integer
> list
) { int list_size = list.size(); // avoid calculating the size every time
for (int i = 0; i < list_size; i++) {
if (integer.compareTo(list.get(i)) >= 0) {
list.add(i, integer);
break;
}
}
}
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuQXJyYXlzOwppbXBvcnQgamF2YS51dGlsLkxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuUmFuZG9tOwoKcHVibGljIGNsYXNzIE1haW4gewoKICAgIHN0YXRpYyBjbGFzcyBSZXN1bHQgewoKICAgICAgICBpbnQgc2l6ZTsKICAgICAgICBsb25nIGxpc3RUaW1lOwogICAgICAgIGxvbmcgYXJyYXlMaXN0VGltZTsKCiAgICAgICAgcHVibGljIFJlc3VsdChpbnQgc2l6ZSkgewogICAgICAgICAgICB0aGlzLnNpemUgPSBzaXplOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB0aHJvd3MgRXhjZXB0aW9uIHsKCiAgICAgICAgLy8gd2FybSB1cCBjb2RlIHRvIG1ha2UgdGhlIGJlbmNobWFyayBtb3JlIGFjY3VyYXRlCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCAxMDA7IGkrKykgewogICAgICAgICAgICBsaXN0VnNBcnJheUxpc3RMaW5lYXJQZXJmb3JtYW5jZSgxMDAwKTsKICAgICAgICB9CgogICAgICAgIC8vIHByaW50IHRoZSBoZWFkZXIKICAgICAgICBTeXN0ZW0ub3V0LnByaW50ZigiJS0xNXMgJS0yMHMgJS0yMHMlbiIsCiAgICAgICAgICAgICAgICAiI0VsZW1lbnRzIiwgIkFycmF5TGlzdCIsICJMaXN0Iik7CgogICAgICAgIC8vIHBlcmZvcm0gdGhlIGJlbmNobWFya2luZwogICAgICAgIHByaW50UmVzdWx0KGxpc3RWc0FycmF5TGlzdExpbmVhclBlcmZvcm1hbmNlKDEwMDApKTsKICAgICAgICBwcmludFJlc3VsdChsaXN0VnNBcnJheUxpc3RMaW5lYXJQZXJmb3JtYW5jZSgyMDA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c3QoSW50ZWdlcltdIGludEFycmF5LCBMaXN0PEludGVnZXI+IGxpc3QpIHsKICAgICAgICBmb3IgKEludGVnZXIgaW50ZWdlciA6IGludEFycmF5KSB7CiAgICAgICAgICAgIGludCBsaXN0X3NpemUgPSBsaXN0LnNpemUoKTsgLy8gYXZvaWQgY2FsY3VsYXRpbmcgdGhlIHNpemUgZXZlcnkgdGltZQogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGxpc3Rfc2l6ZTsgaSsrKSB7CiAgICAgICAgICAgICAgICBpZiAoaW50ZWdlci5jb21wYXJlVG8obGlzdC5nZXQoaSkpID49IDApIHsKICAgICAgICAgICAgICAgICAgICBsaXN0LmFkZChpLCBpbnRlZ2VyKTsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICBwcml2YXRlIHN0YXRpYyB2b2lkIGxpbmVhckluc2VydGlvbkFzQXJyYXlMaXN0KEludGVnZXJbXSBpbnRBcnJheSwgQXJyYXlMaXN0PEludGVnZXI+IGxpc3QpIHsKICAgICAgICBmb3IgKEludGVnZXIgaW50ZWdlciA6IGludEFycmF5KSB7CiAgICAgICAgICAgIGludCBsaXN0X3NpemUgPSBsaXN0LnNpemUoKTsgLy8gYXZvaWQgY2FsY3VsYXRpbmcgdGhlIHNpemUgZXZlcnkgdGltZQogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGxpc3Rfc2l6ZTsgaSsrKSB7CiAgICAgICAgICAgICAgICBpZiAoaW50ZWdlci5jb21wYXJlVG8obGlzdC5nZXQoaSkpID49IDApIHsKICAgICAgICAgICAgICAgICAgICBsaXN0LmFkZChpLCBpbnRlZ2VyKTsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQ==