문제
코드의 기본 프로파일 링을하고 싶지만 C#의 DateTime.Now의 해상도 만 약 16ms 만 발견했습니다. 아직 찾지 못한 구조물을 유지하는 데 더 나은 시간이 있어야합니다.
해결책
다음은 작업 시간에 대한 샘플 비트 코드입니다.
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 클래스는 시스템에서 사용할 수있는 경우 고해상도 카운터를 사용합니다.
다른 팁
System.Diagnostics.StopWatch 클래스는 프로파일 링에 굉장합니다.
다음은 링크입니다 Vance Morrison의 코드 타이머 블로그 자신의 측정 기능을 작성하고 싶지 않은 경우.
최고 해상도 성능 카운터의 경우 기본 Win32 성능 카운터를 사용할 수 있습니다.
다음 p/호출 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()));
Windows의 고해상도 성능 카운터로 호출 할 수 있습니다. 함수 이름은 Kernel32.dll의 QueryPerformAncecounter입니다.
C#으로 가져 오기위한 구문 :
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
Windows의 구문 호출 :
BOOL QueryPerformanceCounter(
LARGE_INTEGER *lpPerformanceCount
);
제휴하지 않습니다 StackOverflow