/// <summary>Stops counting the processor cycles, releases the system resources associated with the cycle counter, and returns the number of cycles elapsed.</summary> /// <param name="cycleCounter">An object representing the state of the cycle counter returned by <see cref="AcquireCycleCounter()" />. The cycle counter should be released only once, and this function invalidates the state object.</param> /// <returns>The number of cycles elapsed since the call to <see cref="AcquireCycleCounter()" />.</returns> /// <exception cref="System.InvalidOperationException">The cycleCounter object is not a valid state of the cycle counter. This can happen if the cycleCounter object was released previously.</exception> /// <exception cref="System.PlatformNotSupportedException">The processor does not have a cycle counter or the operating system does not provide access to the CPU cycle counter</exception> /// <exception cref="System.SystemException">If the attempt to read the cycle counter or release the OS resources failed inside the OS kernel.</exception> /// <seealso cref="AcquireCycleCounter()" /> public static ulong ReleaseCycleCounter(CpuCycleCounterState cycleCounter) { ulong ticks; Status status = yepLibrary_GetCpuCyclesRelease(ref cycleCounter.state, out ticks); if (status != Status.Ok) { throw GetException(status); } return(ticks); }
public static void Main(string[] args) { const int arraySize = 1024 * 4; const int iterationMax = 1000; short[] array = new short[arraySize]; /* Check if the system supports performance counters */ if (Yeppp.Library.IsSupported(Yeppp.CpuSystemFeature.CycleCounter)) { /* Estimate the measurement overhead */ ulong minOverhead = UInt64.MaxValue; /* 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++) { Yeppp.CpuCycleCounterState state = Yeppp.Library.AcquireCycleCounter(); ulong cycles = Yeppp.Library.ReleaseCycleCounter(state); minOverhead = Math.Min(minOverhead, cycles); } /* Now measure the cycles for computation */ ulong minCycles = UInt64.MaxValue; /* 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++) { 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); } /* Subtract the overhead and normalize by the number of elements */ double cpe = ((double)(minCycles - minOverhead)) / ((double)arraySize); Console.WriteLine("Cycles per element: {0:F2}", cpe); } else { Console.WriteLine("Processor cycle counter is not supported"); } }
/// <summary>Stops counting the processor cycles, releases the system resources associated with the cycle counter, and returns the number of cycles elapsed.</summary> /// <param name="cycleCounter">An object representing the state of the cycle counter returned by <see cref="AcquireCycleCounter()" />. The cycle counter should be released only once, and this function invalidates the state object.</param> /// <returns>The number of cycles elapsed since the call to <see cref="AcquireCycleCounter()" />.</returns> /// <exception cref="System.InvalidOperationException">The cycleCounter object is not a valid state of the cycle counter. This can happen if the cycleCounter object was released previously.</exception> /// <exception cref="System.PlatformNotSupportedException">The processor does not have a cycle counter or the operating system does not provide access to the CPU cycle counter</exception> /// <exception cref="System.SystemException">If the attempt to read the cycle counter or release the OS resources failed inside the OS kernel.</exception> /// <seealso cref="AcquireCycleCounter()" /> public static ulong ReleaseCycleCounter(CpuCycleCounterState cycleCounter) { ulong ticks; Status status = yepLibrary_GetCpuCyclesRelease(ref cycleCounter.state, out ticks); if (status != Status.Ok) throw GetException(status); return ticks; }