Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Propiedad Stopwatch.ElapsedMilliseconds

 

Publicado: octubre de 2016

Obtiene el tiempo total transcurrido medido por la instancia actual, en milisegundos.

Espacio de nombres:   System.Diagnostics
Ensamblado:  System (en System.dll)

Public ReadOnly Property ElapsedMilliseconds As Long

Valor de propiedad

Type: System.Int64

Entero largo de solo lectura que representa el número total de milisegundos medido por la instancia actual.

This property represents elapsed time rounded down to the nearest whole millisecond value. For higher precision measurements, use the P:System.Diagnostics.Stopwatch.Elapsed or P:System.Diagnostics.Stopwatch.ElapsedTicks properties.

You can query the properties P:System.Diagnostics.Stopwatch.Elapsed, P:System.Diagnostics.Stopwatch.ElapsedMilliseconds, and P:System.Diagnostics.Stopwatch.ElapsedTicks while the T:System.Diagnostics.Stopwatch instance is running or stopped. The elapsed time properties steadily increase while the T:System.Diagnostics.Stopwatch is running; they remain constant when the instance is stopped.

By default, the elapsed time value of a T:System.Diagnostics.Stopwatch instance equals the total of all measured time intervals. Each call to M:System.Diagnostics.Stopwatch.Start begins counting at the cumulative elapsed time; each call to M:System.Diagnostics.Stopwatch.Stop ends the current interval measurement and freezes the cumulative elapsed time value. Use the M:System.Diagnostics.Stopwatch.Reset method to clear the cumulative elapsed time in an existing T:System.Diagnostics.Stopwatch instance.

The following example uses the T:System.Diagnostics.Stopwatch class to measure the performance of four different implementations for parsing an integer from a string. This code example is part of a larger example provided for the T:System.Diagnostics.Stopwatch class.

   Private Shared Sub TimeOperations()

      Dim nanosecPerTick As Long = 1000000000 / Stopwatch.Frequency
      Const numIterations As Long = 10000

      ' Define the operation title names.
      Dim operationNames As String() =  _
        {"Operation: Int32.Parse(""0"")", _
         "Operation: Int32.TryParse(""0"")", _
         "Operation: Int32.Parse(""a"")", _
         "Operation: Int32.TryParse(""a"")"}

      ' Time four different implementations for parsing 
      ' an integer from a string. 

      Dim operation As Integer
      For operation = 0 To 3
         ' Define variables for operation statistics.
         Dim numTicks As Long = 0
         Dim numRollovers As Long = 0
         Dim maxTicks As Long = 0
         Dim minTicks As Long = Int64.MaxValue
         Dim indexFastest As Integer = - 1
         Dim indexSlowest As Integer = - 1
         Dim milliSec As Long = 0

         Dim time10kOperations As Stopwatch = Stopwatch.StartNew()

         ' Run the current operation 10001 times.
         ' The first execution time will be tossed
         ' out, since it can skew the average time.
         Dim i As Integer
         For i = 0 To numIterations
            Dim ticksThisTime As Long = 0
            Dim inputNum As Integer
            Dim timePerParse As Stopwatch

            Select Case operation
               Case 0
                  ' Parse a valid integer using
                  ' a try-catch statement.
                  ' Start a new stopwatch timer.
                  timePerParse = Stopwatch.StartNew()

                  Try
                     inputNum = Int32.Parse("0")
                  Catch e As FormatException
                     inputNum = 0
                  End Try

                  ' Stop the timer, and save the
                  ' elapsed ticks for the operation.
                  timePerParse.Stop()
                  ticksThisTime = timePerParse.ElapsedTicks
               Case 1
                  ' Parse a valid integer using
                  ' the TryParse statement.
                  ' Start a new stopwatch timer.
                  timePerParse = Stopwatch.StartNew()

                  If Not Int32.TryParse("0", inputNum) Then
                     inputNum = 0
                  End If

                  ' Stop the timer, and save the
                  ' elapsed ticks for the operation.
                  timePerParse.Stop()
                  ticksThisTime = timePerParse.ElapsedTicks
               Case 2
                  ' Parse an invalid value using
                  ' a try-catch statement.
                  ' Start a new stopwatch timer.
                  timePerParse = Stopwatch.StartNew()

                  Try
                     inputNum = Int32.Parse("a")
                  Catch e As FormatException
                     inputNum = 0
                  End Try

                  ' Stop the timer, and save the
                  ' elapsed ticks for the operation.
                  timePerParse.Stop()
                  ticksThisTime = timePerParse.ElapsedTicks
               Case 3
                  ' Parse an invalid value using
                  ' the TryParse statement.
                  ' Start a new stopwatch timer.
                  timePerParse = Stopwatch.StartNew()

                  If Not Int32.TryParse("a", inputNum) Then
                     inputNum = 0
                  End If

                  ' Stop the timer, and save the
                  ' elapsed ticks for the operation.
                  timePerParse.Stop()
                  ticksThisTime = timePerParse.ElapsedTicks

               Case Else
            End Select

            ' Skip over the time for the first operation,
            ' just in case it caused a one-time
            ' performance hit.
            If i = 0 Then
               time10kOperations.Reset()
               time10kOperations.Start()
            Else

               ' Update operation statistics
               ' for iterations 1-10001.
               If maxTicks < ticksThisTime Then
                  indexSlowest = i
                  maxTicks = ticksThisTime
               End If
               If minTicks > ticksThisTime Then
                  indexFastest = i
                  minTicks = ticksThisTime
               End If
               numTicks += ticksThisTime
               If numTicks < ticksThisTime Then
                  ' Keep track of rollovers.
                  numRollovers += 1
               End If
            End If
         Next i

         ' Display the statistics for 10000 iterations.
         time10kOperations.Stop()
         milliSec = time10kOperations.ElapsedMilliseconds

         Console.WriteLine()
         Console.WriteLine("{0} Summary:", operationNames(operation))
         Console.WriteLine("  Slowest time:  #{0}/{1} = {2} ticks", _
            indexSlowest, numIterations, maxTicks)
         Console.WriteLine("  Fastest time:  #{0}/{1} = {2} ticks", _
            indexFastest, numIterations, minTicks)
         Console.WriteLine("  Average time:  {0} ticks = {1} nanoseconds", _
            numTicks / numIterations, numTicks * nanosecPerTick / numIterations)
         Console.WriteLine("  Total time looping through {0} operations: {1} milliseconds", _
            numIterations, milliSec)
      Next operation

   End Sub
End Class

Plataforma universal de Windows
Disponible desde 8
.NET Framework
Disponible desde 2.0
Biblioteca de clases portable
Se admite en: plataformas portátiles de .NET
Windows Phone Silverlight
Disponible desde 7.0
Windows Phone
Disponible desde 8.1
Volver al principio
Mostrar: