Created
March 5, 2015 03:52
-
-
Save amaembo/168fe8636e085fb8341f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.openjdk.jmh.infra.Blackhole; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.annotations.Warmup; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.Param; | |
import org.openjdk.jmh.annotations.Measurement; | |
import org.openjdk.jmh.annotations.OutputTimeUnit; | |
import java.util.concurrent.TimeUnit; | |
import java.util.*; | |
@Warmup(iterations=10) | |
@Measurement(iterations=10) | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.MICROSECONDS) | |
@Fork(3) | |
public class IteratorBench { | |
static int[] ia = new int[10000]; | |
static IntCollection ic; | |
static IntCollection2 ic2; | |
static List<Integer> il = new ArrayList<Integer>(); | |
static { | |
for(int i=0; i<ia.length; i++) ia[i] = i*1337%7331; | |
for(int i=0; i<ia.length; i++) il.add(i*1337%7331); | |
ic = new IntCollection(ia); | |
ic2 = new IntCollection2(ia); | |
} | |
static class IntIterator { | |
IntCollection c; | |
int idx = 0; | |
public IntIterator(IntCollection c) { | |
this.c = c; | |
} | |
public int next() { | |
if(idx >= c.data.length) | |
throw new NoSuchElementException(); | |
return c.data[idx++]; | |
} | |
public boolean hasNext() { | |
return idx < c.data.length; | |
} | |
} | |
static class IntegerIterator implements Iterator<Integer> { | |
IntCollection c; | |
int idx = 0; | |
public IntegerIterator(IntCollection c) { | |
this.c = c; | |
} | |
public Integer next() { | |
if(idx >= c.data.length) | |
throw new NoSuchElementException(); | |
return c.data[idx++]; | |
} | |
public boolean hasNext() { | |
return idx < c.data.length; | |
} | |
public void remove() {} | |
}; | |
static class IntCollection implements Iterable<Integer> { | |
int[] data; | |
public IntCollection(int[] data) { | |
this.data = data.clone(); | |
} | |
public IntIterator intIterator() { | |
return new IntIterator(this); | |
} | |
public final IntegerIterator iterator() { | |
return new IntegerIterator(this); | |
} | |
} | |
static class IntCollection2 implements Iterable<Integer> { | |
int[] data; | |
public IntCollection2(int[] data) { | |
this.data = data.clone(); | |
} | |
public Iterator<Integer> iterator() { | |
return new Iterator<Integer>() { | |
int idx = 0; | |
public Integer next() { | |
if(idx >= data.length) | |
throw new NoSuchElementException(); | |
return new Integer(data[idx++]); | |
} | |
public boolean hasNext() { | |
return idx < data.length; | |
} | |
public void remove() {} | |
}; | |
} | |
} | |
public int sumArray(int[] arr) { | |
int sum = 0; | |
for(int val : arr) sum+=val; | |
return sum; | |
} | |
public int sumIntIterator(IntCollection ic) { | |
int sum = 0; | |
for(IntIterator it = ic.intIterator(); it.hasNext(); ) sum+=it.next(); | |
return sum; | |
} | |
public int sumIterator(IntCollection ic) { | |
int sum = 0; | |
for(int val : ic) sum+=val; | |
return sum; | |
} | |
public int sumIterator(IntCollection2 ic) { | |
int sum = 0; | |
for(int val : ic) sum+=val; | |
return sum; | |
} | |
public int sumIteratorAbstract(Iterable<Integer> ic) { | |
int sum = 0; | |
for(int val : ic) sum+=val; | |
return sum; | |
} | |
@Benchmark | |
public void testArray(Blackhole bh) { | |
bh.consume(sumArray(ia)); | |
} | |
@Benchmark | |
public void testIntIterator(Blackhole bh) { | |
bh.consume(sumIntIterator(ic)); | |
} | |
@Benchmark | |
public void testIterator(Blackhole bh) { | |
bh.consume(sumIterator(ic)); | |
} | |
@Benchmark | |
public void testIteratorAbstract(Blackhole bh) { | |
bh.consume(sumIteratorAbstract(ic)); | |
} | |
@Benchmark | |
public void testIterator2(Blackhole bh) { | |
bh.consume(sumIterator(ic2)); | |
} | |
@Benchmark | |
public void testIterator2Abstract(Blackhole bh) { | |
bh.consume(sumIteratorAbstract(ic2)); | |
} | |
@Benchmark | |
public void testIteratorList(Blackhole bh) { | |
bh.consume(sumIteratorAbstract(il)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment