Stopwatch.ElapsedTicks Propriedade

Definição

Obtém o tempo decorrido total medido pela instância atual, em tiques de temporizador.

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

Valor da propriedade

Um inteiro longo somente leitura que representa o número total de tiques de temporizador medidos pela instância atual.

Exemplos

O exemplo a seguir usa a Stopwatch classe para medir o desempenho de quatro implementações diferentes para analisar um inteiro de uma cadeia de caracteres. Este exemplo de código faz parte de um exemplo maior fornecido para a 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

Comentários

Essa propriedade representa o número de tiques decorridos no mecanismo de temporizador subjacente. Um tique é a menor unidade de tempo que o Stopwatch temporizador pode medir. Use o Frequency campo para converter o ElapsedTicks valor em um número de segundos.

Você pode consultar as propriedades ElapsedeElapsedMilliseconds, ElapsedTicksenquanto a Stopwatch instância estiver em execução ou interrompida. As propriedades de tempo decorrido aumentam constantemente durante a execução Stopwatch ; elas permanecem constantes quando a instância é interrompida.

Por padrão, o valor de tempo decorrido de uma Stopwatch instância é igual ao total de todos os intervalos de tempo medidos. Cada chamada para começar a Start contar no tempo decorrido cumulativo; cada chamada para Stop encerrar a medida do intervalo atual e congela o valor de tempo decorrido cumulativo. Use o Reset método para limpar o tempo decorrido cumulativo em uma instância existente Stopwatch .

Note

Stopwatch tiques são diferentes de DateTime.Ticks. Cada tique no DateTime.Ticks valor representa um intervalo de 100 nanossegundos. Cada tique no ElapsedTicks valor representa o intervalo de tempo igual a 1 segundo dividido pelo Frequency.

Aplica-se a

Confira também