Timer ad alta risoluzione in .NET
Domanda
Vorrei fare un po 'di profilazione di base del mio codice, ma ho scoperto che DateTime.Now in C # ha solo una risoluzione di circa 16 ms. Deve esserci tempo migliore per mantenere i costrutti che non ho ancora trovato.
Soluzione
Ecco un esempio di codice per programmare un'operazione:
Dim sw As New Stopwatch()
sw.Start()
//Insert Code To Time
sw.Stop()
Dim ms As Long = sw.ElapsedMilliseconds
Console.WriteLine("Total Seconds Elapsed: " & ms / 1000)
EDIT:
E la cosa bella è che può riprendere anche.
Stopwatch sw = new Stopwatch();
foreach(MyStuff stuff in _listOfMyStuff)
{
sw.Start();
stuff.DoCoolCalculation();
sw.Stop();
}
Console.WriteLine("Total calculation time: {0}", sw.Elapsed);
La classe System.Diagnostics.Stopwatch sarà usa un contatore ad alta risoluzione se disponibile sul tuo sistema.
Altri suggerimenti
La classe System.Diagnostics.StopWatch è eccezionale per la creazione di profili.
Ecco un link a Blog Timer codice di Vance Morrison se non si desidera scrivere le proprie funzioni di misurazione.
Per i contatori delle prestazioni con la massima risoluzione è possibile utilizzare i contatori delle prestazioni win32 sottostanti.
Aggiungi i seguenti sigilli P / Invoke:
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
public static extern bool QueryPerformanceCounter(out long perfcount);
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
public static extern bool QueryPerformanceFrequency(out long freq);
E chiamali usando:
#region Query Performance Counter
/// <summary>
/// Gets the current 'Ticks' on the performance counter
/// </summary>
/// <returns>Long indicating the number of ticks on the performance counter</returns>
public static long QueryPerformanceCounter()
{
long perfcount;
QueryPerformanceCounter(out perfcount);
return perfcount;
}
#endregion
#region Query Performance Frequency
/// <summary>
/// Gets the number of performance counter ticks that occur every second
/// </summary>
/// <returns>The number of performance counter ticks that occur every second</returns>
public static long QueryPerformanceFrequency()
{
long freq;
QueryPerformanceFrequency(out freq);
return freq;
}
#endregion
Scarica tutto in una semplice classe e sei pronto per iniziare. Esempio (assumendo un nome di classe di PerformanceCounters):
long startCount = PerformanceCounter.QueryPerformanceCounter();
// DoStuff();
long stopCount = PerformanceCounter.QueryPerformanceCounter();
long elapsedCount = stopCount - startCount;
double elapsedSeconds = (double)elapsedCount / PerformanceCounter.QueryPerformanceFrequency();
MessageBox.Show(String.Format("Took {0} Seconds", Math.Round(elapsedSeconds, 6).ToString()));
È possibile richiamare il contatore delle prestazioni ad alta risoluzione in Windows. Il nome della funzione è QueryPerformanceCounter in kernel32.dll.
Sintassi per l'importazione in C #:
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
Sintassi per chiamata Windows:
BOOL QueryPerformanceCounter(
LARGE_INTEGER *lpPerformanceCount
);