Stopwatch.ElapsedTicks Eigenschaft

Definition

Ruft die gesamte verstrichene Zeit, die von der aktuellen Instanz gemessen wurde, in Timerintervallen (Ticks) ab.

public:
 property long ElapsedTicks { long get(); };
public long ElapsedTicks { get; }
member this.ElapsedTicks : int64
Public ReadOnly Property ElapsedTicks As Long

Eigenschaftswert

Eine schreibgeschützte Long-Integer-Zahl, die die Gesamtanzahl der von der aktuellen Instanz gemessenen Zeitgeberintervalle angibt.

Beispiele

Im folgenden Beispiel wird die Stopwatch -Klasse verwendet, um die Leistung von vier verschiedenen Implementierungen für die Analyse einer ganzen Zahl aus einer Zeichenfolge zu messen. Dieses Codebeispiel ist Teil eines größeren Beispiels, das für die Stopwatch-Klasse bereitgestellt wird.

Int64 ticksThisTime = 0;
int inputNum;
Stopwatch ^ timePerParse;
switch ( 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 ( FormatException^ ) 
      {
         inputNum = 0;
      }

      // Stop the timer, and save the
      // elapsed ticks for the operation.
      timePerParse->Stop();
      ticksThisTime = timePerParse->ElapsedTicks;
      break;

   case 1:
      
      // Parse a valid integer using
      // the TryParse statement.
      // Start a new stopwatch timer.
      timePerParse = Stopwatch::StartNew();
      if (  !Int32::TryParse( "0", inputNum ) )
      {
         inputNum = 0;
      }
      
      // Stop the timer, and save the
      // elapsed ticks for the operation.
      timePerParse->Stop();
      ticksThisTime = timePerParse->ElapsedTicks;
      break;

   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 ( FormatException^ ) 
      {
         inputNum = 0;
      }

      // Stop the timer, and save the
      // elapsed ticks for the operation.
      timePerParse->Stop();
      ticksThisTime = timePerParse->ElapsedTicks;
      break;

   case 3:
      
      // Parse an invalid value using
      // the TryParse statement.
      // Start a new stopwatch timer.
      timePerParse = Stopwatch::StartNew();
      if (  !Int32::TryParse( "a", inputNum ) )
      {
         inputNum = 0;
      }
      
      // Stop the timer, and save the
      // elapsed ticks for the operation.
      timePerParse->Stop();
      ticksThisTime = timePerParse->ElapsedTicks;
      break;

   default:
      break;
}
long ticksThisTime = 0;
int inputNum;
Stopwatch timePerParse;

switch (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 (FormatException)
        {
            inputNum = 0;
        }

        // Stop the timer, and save the
        // elapsed ticks for the operation.

        timePerParse.Stop();
        ticksThisTime = timePerParse.ElapsedTicks;
        break;
    case 1:
        // Parse a valid integer using
        // the TryParse statement.

        // Start a new stopwatch timer.
        timePerParse = Stopwatch.StartNew();

        if (!Int32.TryParse("0", out inputNum))
        {
            inputNum = 0;
        }

        // Stop the timer, and save the
        // elapsed ticks for the operation.
        timePerParse.Stop();
        ticksThisTime = timePerParse.ElapsedTicks;
        break;
    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 (FormatException)
        {
            inputNum = 0;
        }

        // Stop the timer, and save the
        // elapsed ticks for the operation.
        timePerParse.Stop();
        ticksThisTime = timePerParse.ElapsedTicks;
        break;
    case 3:
        // Parse an invalid value using
        // the TryParse statement.

        // Start a new stopwatch timer.
        timePerParse = Stopwatch.StartNew();

        if (!Int32.TryParse("a", out inputNum))
        {
            inputNum = 0;
        }

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

    default:
        break;
}
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

Hinweise

Diese Eigenschaft stellt die Anzahl verstrichener Ticks im zugrunde liegenden Timermechanismus dar. Ein Tick ist die kleinste Zeiteinheit, die der Stopwatch Timer messen kann. Verwenden Sie das Frequency Feld, um den ElapsedTicks Wert in eine Anzahl von Sekunden zu konvertieren.

Sie können die Eigenschaften Elapsed, ElapsedMillisecondsund ElapsedTicks abfragen, während die Stopwatch instance ausgeführt oder beendet wird. Die verstrichenen Zeiteigenschaften erhöhen sich kontinuierlich, während die Stopwatch ausgeführt wird. Sie bleiben konstant, wenn die instance beendet wird.

Standardmäßig entspricht der verstrichene Zeitwert einer Stopwatch instance der Summe aller gemessenen Zeitintervalle. Jeder Aufruf von Start beginnt mit dem Zählen zur kumulativen verstrichenen Zeit. Jeder Aufruf beendet Stop die aktuelle Intervallmessung und friert den kumulativen verstrichenen Zeitwert ein. Verwenden Sie die Reset -Methode, um die kumulative verstrichene Zeit in einer vorhandenen Stopwatch instance zu löschen.

Hinweis

Stopwatch Ticks unterscheiden sich von DateTime.Ticks. Jeder Tick im DateTime.Ticks Wert stellt ein Intervall von 100 Nanosekunden dar. Jeder Tick im ElapsedTicks Wert stellt das Zeitintervall von 1 Sekunde dividiert durch dar Frequency.

Gilt für:

Weitere Informationen