Created
October 26, 2013 11:02
-
-
Save szarnekow/7168147 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 com.google.caliper.Benchmark; | |
import com.google.caliper.Param; | |
import com.google.caliper.api.Macrobenchmark; | |
import com.google.caliper.runner.CaliperMain; | |
import java.util.AbstractList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class IntArraySumBenchmark extends Benchmark { | |
@Param({"10", "1000", "1000000", "5000000"}) | |
private int length; | |
private int[] array; | |
@Override protected void setUp() { | |
array = new int[length]; | |
for(int i = 0; i < length; i++) { | |
array[i] = 3 * i; | |
} | |
} | |
public int timeSequentialSum(int reps) { | |
int result = 0; | |
for (int i = 0; i < reps; i++) { | |
int j = 0; | |
for(int k : array) { | |
j += k * 5 * i; | |
} | |
result += j; | |
} | |
return result; | |
} | |
public int timeSequentialSumJava8(int reps) { | |
int result = 0; | |
for (int i = 0; i < reps; i++) { | |
final int i_ = i; | |
result += Arrays.stream(array).map(e -> e * 5 * i_).sum(); | |
} | |
return result; | |
} | |
public int timeParallelSumJava8(int reps) { | |
int result = 0; | |
for (int i = 0; i < reps; i++) { | |
final int i_ = i; | |
result += Arrays.stream(array).parallel().map(e -> e * 5 * i_).sum(); | |
} | |
return result; | |
} | |
public static void main(String[] args) throws Exception { | |
CaliperMain.main(IntArraySumBenchmark.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment