Stopwatch.ElapsedTicks Propriété

Définition

Obtient le temps écoulé total mesuré par l’instance actuelle, en cycles du minuteur.

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

Valeur de propriété

Entier long en lecture seule représentant le nombre total de graduations du minuteur mesurées par l’instance actuelle.

Exemples

L’exemple suivant utilise la Stopwatch classe pour mesurer les performances de quatre implémentations différentes pour analyser un entier à partir d’une chaîne. Cet exemple de code fait partie d’un exemple plus grand fourni pour la Stopwatch classe.

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

Remarques

Cette propriété représente le nombre de cycles écoulés dans le mécanisme du minuteur sous-jacent. Une graduation est la plus petite unité de temps que le Stopwatch minuteur peut mesurer. Utilisez le Frequency champ pour convertir la ElapsedTicks valeur en un certain nombre de secondes.

Vous pouvez interroger les propriétés Elapsed, ElapsedMillisecondset ElapsedTicks pendant l’exécution ou l’arrêt de l’instance Stopwatch . Les propriétés de temps écoulés augmentent régulièrement pendant l’exécution Stopwatch ; elles restent constantes lorsque l’instance est arrêtée.

Par défaut, la valeur de temps écoulé d’une Stopwatch instance est égale au total de tous les intervalles de temps mesurés. Chaque appel à commencer à Start compter au moment cumulé écoulé ; chaque appel pour mettre fin à Stop la mesure de l’intervalle actuel et fige la valeur de temps écoulé cumulée. Utilisez la Reset méthode pour effacer le temps cumulé écoulé dans une instance existante Stopwatch .

Note

Stopwatch les graduations sont différentes de DateTime.Ticks. Chaque graduation de la DateTime.Ticks valeur représente un intervalle de 100 nanosecondes. Chaque graduation de la ElapsedTicks valeur représente l’intervalle de temps égal à 1 seconde divisé par le Frequency.

S’applique à

Voir aussi