vs2017 visual studio net .net profiling timer

.net - visual - profiler vs2017



Temporizador de alta resoluciĆ³n en.NET (4)

La clase System.Diagnostics.StopWatch es increíble para crear perfiles.

Aquí hay un enlace al Code Timer Blog de Vance Morrison si no desea escribir sus propias funciones de medición.

Me gustaría hacer un perfil básico de mi código, pero descubrí que DateTime.Now en C # solo tiene una resolución de alrededor de 16 ms. Debe haber mejor tiempo para mantener construcciones que aún no he encontrado.


Para los contadores de rendimiento de mayor resolución, puede usar los contadores de rendimiento win32 subyacentes.

Agregue las siguientes 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);

Y llámalos 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

Vuelque todo en una clase simple y listo. Ejemplo (suponiendo un nombre de clase de 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()));


Puede llamar al contador de rendimiento de alta resolución en Windows. El nombre de la función es QueryPerformanceCounter en kernel32.dll.

Sintaxis para importar a C #:

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

Sintaxis para llamadas de Windows:

BOOL QueryPerformanceCounter( LARGE_INTEGER *lpPerformanceCount );

QueryPerformanceCounter @ MSDN


Aquí hay una muestra de código para cronometrar una operación:

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)

EDITAR:

Y lo bueno es que también puede reanudarse.

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

La clase System.Diagnostics.Stopwatch usará un contador de alta resolución si hay uno disponible en su sistema.