コードの基本的なプロファイリングを行いたいのですが、C# の DateTime.Now の分解能が約 16 ミリ秒しかないことがわかりました。私がまだ見つけていない構造を維持するためのより良い時間があるに違いありません。
20580 次
4 に答える
55
操作のタイミングを計るサンプル コードを次に示します。
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)
編集:
そして素晴らしいことは、それも再開できるということです。
Stopwatch sw = new Stopwatch();
foreach(MyStuff stuff in _listOfMyStuff)
{
sw.Start();
stuff.DoCoolCalculation();
sw.Stop();
}
Console.WriteLine("Total calculation time: {0}", sw.Elapsed);
System.Diagnostics.Stopwatchクラスは、システムで使用可能な場合、高解像度カウンターを使用します。
于 2008-10-02T15:43:21.643 に答える
20
System.Diagnostics.StopWatch クラスはプロファイリングに最適です。
独自の測定関数を作成したくない場合は、 Vance Morrison の Code Timer Blogへのリンクを次に示します。
于 2008-10-02T15:31:12.593 に答える
8
最高解像度のパフォーマンス カウンターについては、基になる win32 パフォーマンス カウンターを使用できます。
次の P/Invoke sig を追加します。
[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);
そして、次を使用してそれらを呼び出します。
#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
すべてを単純なクラスにダンプすれば、準備完了です。例 (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()));
于 2008-10-02T15:48:37.467 に答える
1
Windows の高解像度パフォーマンス カウンターを呼び出すことができます。関数名は、kernel32.dll の QueryPerformanceCounter です。
C# にインポートするための構文:
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
Windows 呼び出しの構文:
BOOL QueryPerformanceCounter(
LARGE_INTEGER *lpPerformanceCount
);
于 2008-10-02T15:42:31.070 に答える