Created
January 27, 2014 14:40
-
-
Save Fuud/8649674 to your computer and use it in GitHub Desktop.
This file contains 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
package quantum.selfstudy4; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.GenerateMicroBenchmark; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.OperationsPerInvocation; | |
import org.openjdk.jmh.annotations.OutputTimeUnit; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import quantum.util.Utils; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* Quantum Performance Effects (demo) | |
* | |
* get and try to explain performance results | |
* | |
* @author Sergey Kuksenko | |
*/ | |
@State | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@BenchmarkMode(Mode.AverageTime) | |
public class LoopBack { | |
public static final int K = 1024; | |
public static final int M = K * K; | |
private int[] arr1K; | |
private int[] arr32K; | |
private int[] arr4M; | |
@Setup | |
public void initI() { | |
arr1K = Utils.newRandomIntArray(K); | |
arr32K = Utils.newRandomIntArray(32 * K); | |
arr4M = Utils.newRandomIntArray(4 * M); | |
} | |
public static int foreachSum(int[] arr) { | |
int s = 0; | |
for (int x : arr) { | |
s += x; | |
} | |
return s; | |
} | |
public static int forwardSum(int[] arr) { | |
int s = 0; | |
for (int i = 0; i < arr.length; i++) { | |
s += arr[i]; | |
} | |
return s; | |
} | |
public static int forwardRange(int[] arr) { | |
int s = 0; | |
for (int i : Range.from(0).to(arr.length-1)) { | |
s += arr[i]; | |
} | |
return s; | |
} | |
public static int backwardSum(int[] arr) { | |
int s = 0; | |
for (int i = arr.length - 1; i >= 0; i--) { | |
s += arr[i]; | |
} | |
return s; | |
} | |
@GenerateMicroBenchmark | |
@OperationsPerInvocation(K) | |
public int foreach1K() { | |
return foreachSum(arr1K); | |
} | |
@GenerateMicroBenchmark | |
@OperationsPerInvocation(K) | |
public int forward1K() { | |
return forwardSum(arr1K); | |
} | |
@GenerateMicroBenchmark | |
@OperationsPerInvocation(K) | |
public int forwardRange1K() { | |
return forwardRange(arr1K); | |
} | |
@GenerateMicroBenchmark | |
@OperationsPerInvocation(K) | |
public int backward1K() { | |
return backwardSum(arr1K); | |
} | |
@GenerateMicroBenchmark | |
@OperationsPerInvocation(32 * K) | |
public int foreach32K() { | |
return foreachSum(arr32K); | |
} | |
@GenerateMicroBenchmark | |
@OperationsPerInvocation(32 * K) | |
public int forward32K() { | |
return forwardSum(arr32K); | |
} | |
@GenerateMicroBenchmark | |
@OperationsPerInvocation(32 * K) | |
public int forwardRange32K() { | |
return forwardRange(arr32K); | |
} | |
@GenerateMicroBenchmark | |
@OperationsPerInvocation(32 * K) | |
public int backward32K() { | |
return backwardSum(arr32K); | |
} | |
@GenerateMicroBenchmark | |
@OperationsPerInvocation(4 * M) | |
public int foreach4M() { | |
return foreachSum(arr4M); | |
} | |
@GenerateMicroBenchmark | |
@OperationsPerInvocation(4 * M) | |
public int forward4M() { | |
return forwardSum(arr4M); | |
} | |
@GenerateMicroBenchmark | |
@OperationsPerInvocation(4 * M) | |
public int forwardRange4M() { | |
return forwardSum(arr4M); | |
} | |
@GenerateMicroBenchmark | |
@OperationsPerInvocation(4 * M) | |
public int backward4M() { | |
return backwardSum(arr4M); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment