Yeppp!
 All Classes Functions Variables Pages
CpuCycles.java

This example shows how to use Yeppp! library to measure the execution time in CPU cycles:

import java.util.Random;
class CpuCycles {
public static void main(String[] args) {
final int arraySize = 1024*4;
final int iterationMax = 1000;
final short[] array = new short[arraySize];
/* Check if the system supports performance counters */
System.out.println(info.yeppp.CpuSystemFeature.CycleCounter.toString());
if (info.yeppp.Library.isSupported(info.yeppp.CpuSystemFeature.CycleCounter)) {
/* Estimate the measurement overhead */
long minOverhead = Long.MAX_VALUE;
/* Repeat many times and take the minimum to filter out noise from interrupts and caches/branch prediction/page faults */
for (int iteration = 0; iteration < iterationMax; iteration++) {
final info.yeppp.CpuCycleCounterState state = info.yeppp.Library.acquireCycleCounter();
final long cycles = info.yeppp.Library.releaseCycleCounter(state);
minOverhead = Math.min(minOverhead, cycles);
}
/* Now measure the cycles for computation */
long minCycles = Long.MAX_VALUE;
/* Repeat many times and take the minimum to filter out noise from interrupts and caches/branch prediction/page faults */
for (int iteration = 0; iteration < iterationMax; iteration++) {
final info.yeppp.CpuCycleCounterState state = info.yeppp.Library.acquireCycleCounter();
final Random rng = new Random(42l);
for (int i = 0; i < arraySize; i++) {
array[i] = (short)rng.nextInt();
}
final long cycles = info.yeppp.Library.releaseCycleCounter(state);
minCycles = Math.min(minCycles, cycles);
}
/* Subtract the overhead and normalize by the number of elements */
final double cpe = ((double)(minCycles - minOverhead)) / ((double)arraySize);
System.out.println(String.format("Cycles per element: %3.2f\n", cpe));
} else {
System.out.println("Processor cycle counter is not supported");
}
}
}