/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This is not a regression test, but a micro-benchmark.
* Be patient; this runs for half an hour!
*
* I have run this as follows:
*
* for f in -client -server; do mergeBench dolphin . jr -dsa -da $f IteratorMicroBenchmark.java; done
*
*
* @author Martin Buchholz
*/
public class IteratorMicroBenchmark {
abstract static class Job {
}
private static void collectAllGarbage() {
try {
} catch (InterruptedException e) { throw new Error(e); }
}
/**
* Runs each job for long enough that all the runtime compilers
* have had plenty of time to warm up, i.e. get around to
* compiling everything worth compiling.
* Returns array of average times per job per run.
*/
long t;
int j = 0;
nanoss[i] = t/j;
}
return nanoss;
}
}
// Print out absolute and relative times, calibrated against first job
}
return null;
}
}
}
int n = 0;
// Arrays.copyOf not available in JDK 5
return ret;
}
if (sum == 42)
}
}
return new Iterable<T>() {
return new Iterator<T>() {
}
/**
* Usage: [iterations=N] [size=N] [filter=REGEXP]
*/
// Populate collections with random data
for (int i = 0; i < size; i++) {
}
// Also test "short" collections
final int shortSize = 5;
// Checks for correctness *and* prevents loop optimizations
class Check {
private int sum;
if (this.sum == 0)
throw new AssertionError("Sum mismatch");
}
}
// new Job("Vector iterate desugared") {
// public void work() throws Throwable {
// for (int i = 0; i < iterations; i++) {
// int sum = 0;
// for (Iterator<Integer> it = v.iterator(); it.hasNext();)
// sum += it.next();
// check.sum(sum);}}},
new Job("array loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
for (int j = 0; j < size; ++j)
sum += a[j];
new Job("Vector get loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
for (int j = 0; j < size; ++j)
new Job("Vector iterate for loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
for (Integer n : v)
sum += n;
new Job("Vector descending listIterator loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
while (it.hasPrevious())
new Job("Vector Enumeration loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
while (it.hasMoreElements())
new Job("Vector subList iterate for loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
sum += n;
new Job("Vector subList subList subList iterate for loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
sum += n;
new Job("Vector backwards wrapper ListIterator for loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
sum += n;
new Job("Vector backwards wrapper subList ListIterator for loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
sum += n;
// new Job("Vector iterate for loop invokeinterface") {
// public void work() throws Throwable {
// final List<Integer> l = v;
// for (int i = 0; i < iterations; i++) {
// int sum = 0;
// for (Integer n : l)
// sum += n;
// check.sum(sum);}}},
// new Job("Vector subList iterate for loop invokeinterface") {
// public void work() throws Throwable {
// final List<Integer> l = v;
// for (int i = 0; i < iterations; i++) {
// int sum = 0;
// for (Integer n : asSubList(l))
// sum += n;
// check.sum(sum);}}},
new Job("Short Vector get loop") {
int sum = 0;
for (int j = 0; j < size; ++j)
new Job("Short Vector iterate for loop") {
int sum = 0;
sum += n;
new Job("Short Vector sublist iterate for loop") {
int sum = 0;
sum += n;
new Job("ArrayList get loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
for (int j = 0; j < size; ++j)
new Job("ArrayList iterate for loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
sum += n;
new Job("ArrayList descending listIterator loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
while (it.hasPrevious())
new Job("ArrayList listIterator loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
new Job("ArrayList subList get loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
for (int j = 0; j < size; ++j)
new Job("ArrayList subList iterate for loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
sum += n;
new Job("ArrayList subList subList subList iterate for loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
sum += n;
new Job("ArrayList backwards wrapper ListIterator for loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
sum += n;
new Job("ArrayList backwards wrapper subList ListIterator for loop") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
sum += n;
// new Job("ArrayList iterate desugared") {
// public void work() throws Throwable {
// for (int i = 0; i < iterations; i++) {
// int sum = 0;
// for (Iterator<Integer> it = al.iterator(); it.hasNext();)
// sum += it.next();
// check.sum(sum);}}},
new Job("Short ArrayList get loop") {
int sum = 0;
for (int j = 0; j < size; ++j)
new Job("Short ArrayList iterate for loop") {
int sum = 0;
sum += n;
new Job("Short ArrayList sublist iterate for loop") {
int sum = 0;
sum += n;
new Job("Vector ArrayList alternating iteration") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
new Job("Vector ArrayList alternating invokeVirtual iteration") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
new Job("ConcurrentSkipListMap entrySet iterate") {
for (int i = 0; i < iterations; i++) {
int sum = 0;
deoptimize(sum);}}}
};
}
}