Frage

Ich mag einige grundlegende Profilierung von meinem Code tun, fand aber, dass die DateTime.Now in C # nur eine Auflösung von etwa 16 ms haben. Es muss eine bessere Zeit zu halten Konstrukte sein, dass ich noch nicht gefunden.

War es hilfreich?

Lösung

Hier ist ein Beispiel Stück Code zu Zeit eine Operation:

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:

Und die nette Sache ist, dass es auch wieder aufgenommen werden kann.

Stopwatch sw = new Stopwatch();
foreach(MyStuff stuff in _listOfMyStuff)
{
    sw.Start();
    stuff.DoCoolCalculation();
    sw.Stop();
}
Console.WriteLine("Total calculation time: {0}", sw.Elapsed);

Die System.Diagnostics.Stopwatch Klasse verwenden, um einen hochauflösenden Zähler, wenn man auf dem System verfügbar ist.

Andere Tipps

Die System.Diagnostics.Stopwatch Klasse ist genial für die Profilerstellung.

Hier ist ein Link href="http://blogs.msdn.com/vancem/archive/2006/09/21/765648.aspx" zum Vance Morrison-Code Timer Blog , wenn Sie nicht über Ihre eigenen Messfunktionen schreiben.

Für höchste Auflösung Leistungsindikatoren können Sie die zugrunde liegenden win32 Leistungsindikatoren verwenden.

Fügen Sie den folgenden P / Invoke sigs:

[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);

Und nennen sie mit:

#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

Dump alles in einer einfachen Klasse und Sie sind bereit zu gehen. Beispiel (einen Klassennamen von Performancecounters vorausgesetzt):

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()));

könnten Sie rufen bis auf den hochauflösenden Leistungsindikatoren in Windows. Der Funktionsname ist in kernel32.dll Queryperformancecounter.

Syntax für den Import in C #:

[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);

Syntax für Windows Aufruf:

BOOL QueryPerformanceCounter(      
    LARGE_INTEGER *lpPerformanceCount
);

Queryperformancecounter @ MSDN

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top