This example shows how to use Yeppp! library to measure the execution time in CPU cycles:
using System;
class CpuCycles {
public static void Main(string[] args)
{
const int arraySize = 1024*4;
const int iterationMax = 1000;
short[] array = new short[arraySize];
if (Yeppp.Library.IsSupported(Yeppp.CpuSystemFeature.CycleCounter)) {
ulong minOverhead = UInt64.MaxValue;
for (int iteration = 0; iteration < iterationMax; iteration++) {
Yeppp.CpuCycleCounterState state = Yeppp.Library.AcquireCycleCounter();
ulong cycles = Yeppp.Library.ReleaseCycleCounter(state);
minOverhead = Math.Min(minOverhead, cycles);
}
ulong minCycles = UInt64.MaxValue;
for (int iteration = 0; iteration < iterationMax; iteration++) {
Yeppp.CpuCycleCounterState state = Yeppp.Library.AcquireCycleCounter();
Random rng = new Random();
for (int i = 0; i < arraySize; i++) {
array[i] = (short)rng.Next();
}
ulong cycles = Yeppp.Library.ReleaseCycleCounter(state);
minCycles = Math.Min(minCycles, cycles);
}
double cpe = ((double)(minCycles - minOverhead)) / ((double)arraySize);
Console.WriteLine("Cycles per element: {0:F2}", cpe);
} else {
Console.WriteLine("Processor cycle counter is not supported");
}
}
}