2011-12-27 13 views
5

Necesito analizar la sobrecarga de rendimiento de mi lógica programada usando C# debugger. Entonces necesito comparar dos lógicas en mi código. No quiero instalar complementos como Analyze en mi VisualStudio. Quiero analizar el módulo escribiendo funciones especiales. ¿Tenemos alguna de estas funciones predefinidas disponibles en C#? Necesito todas las opciones disponibles para probar que un módulo es BUENO (bueno, quiero decir que lleva más tiempo ejecutarlo) FYI Uso la edición VisualStudio 2010 Professional.Averigüe cuántos milisegundos tiene que ejecutar un programa de C# en su depurador

+3

No puede utilizar un depurador para medir la Potencia, se ralentiza programar abajo. Use un perfilador. Compró la licencia incorrecta si no desea usar complementos, VS Prof no tiene un generador de perfiles. –

+1

Hay otros perfiles disponibles, como RedGate ANTS. La mayoría de estos tienen versiones de prueba si quieres darles una oportunidad. –

Respuesta

10

Si solo quiere medir el tiempo que necesita ejecutar una función, puede usar la clase Stopwatch.

muestra:

Stopwatch stopWatch = new Stopwatch(); 
stopWatch.Start(); 
CallYourFunction(); 
stopWatch.Stop(); 
// Get the elapsed time as a TimeSpan value. 
TimeSpan ts = stopWatch.Elapsed; 
2

Hacer uso de cronómetro clase portarlo en System.Diagnostics namesapce

Stopwatch stopWatch = new Stopwatch(); 
stopWatch.Start(); 
//instead of this there is line of code that you are going to execute 
Thread.Sleep(10000); 
stopWatch.Stop(); 
// Get the elapsed time as a TimeSpan value. 
TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds/10); 

entrada completa: Get time of Code Execution Using StopWatch

1

puede escribir una función como esta

CalculateTime() 
    { 
     //create timer 
     Stopwatch sw = new Stopwatch(); 
     //start measuring time 
     sw.Start(); 
     //your logic you want to measure 
     //stop recording time 
     sw.Stop(); 

     //you can calculate result using sw.ElapsedTicks; 
    } 
3

La herramienta habitual para este trabajo es el generador de perfiles. Si tiene la edición Ultimate o Premium de Visual Studio 2010, puede usar el generador de perfiles integrado siguiendo estas instrucciones: Beginners Guide to Performance Profiling.

simplemente para medir el tiempo con precisión en una aplicación .NET, puede utilizar Stopwatch:

// you usually need a lot of iterations to get a stable and accurate measurement 
int iterations = 10000; 
Stopwatch stopwatch = Stopwatch.StartNew(); 

// It is important to do as little as possible between starting the 
// stopwatch and calling your function. If you need to allocate memory 
// or do any startup actions, do them before you start. 
for (int i = 0; i < iterations; ++i) 
{ 
    YourFunction(); 
} 

// Similarly, don't do anything more after your code is done, just get 
// the elapsed time immediately. 
TimeSpan totalDuration = stopwatch.Elapsed; 
TimeSpan durationForEachIteration = 
    TimeSpan.FromTicks(totalDuration.Ticks/iterations); 
Cuestiones relacionadas