Stopwatch.ElapsedTicks 属性

定义

获取当前实例测量得出的总运行时间(用计时器计时周期表示)。Gets the total elapsed time measured by the current instance, in timer ticks.

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

属性值

一个只读长整型,表示当前实例测量得出的计时器计时周期的总数。A read-only long integer representing the total number of timer ticks measured by the current instance.

示例

下面的示例使用Stopwatch类来测量四个不同实现的性能, 用于分析字符串中的整数。The following example uses the Stopwatch class to measure the performance of four different implementations for parsing an integer from a string. 此代码示例是为Stopwatch类提供的更大示例的一部分。This code example is part of a larger example provided for the Stopwatch class.

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

注解

此属性表示基础计时器机制中经过的计时周期数。This property represents the number of elapsed ticks in the underlying timer mechanism. 勾选标记是Stopwatch计时器可度量的最小时间单位。A tick is the smallest unit of time that the Stopwatch timer can measure. 使用字段将ElapsedTicks值转换为秒数。 FrequencyUse the Frequency field to convert the ElapsedTicks value into a number of seconds.

可以在Elapsed Stopwatch实例正在运行或ElapsedMilliseconds已停止ElapsedTicks时查询属性、和。You can query the properties Elapsed, ElapsedMilliseconds, and ElapsedTicks while the Stopwatch instance is running or stopped. 运行的Stopwatch时间属性在运行时将持续增加; 当实例停止时, 属性将保持不变。The elapsed time properties steadily increase while the Stopwatch is running; they remain constant when the instance is stopped.

默认情况下, Stopwatch实例的运行时间值等于所有测量的时间间隔的总计。By default, the elapsed time value of a Stopwatch instance equals the total of all measured time intervals. 每次调用Start都会在累计运行时间进行计数; 每次Stop调用都将结束当前间隔度量, 并冻结累计运行时间值。Each call to Start begins counting at the cumulative elapsed time; each call to Stop ends the current interval measurement and freezes the cumulative elapsed time value. 使用方法清除现有Stopwatch实例中的累计运行时间。 ResetUse the Reset method to clear the cumulative elapsed time in an existing Stopwatch instance.

备注

Stopwatch刻度与DateTime.Ticks不同。Stopwatch ticks are different from DateTime.Ticks. 值中的DateTime.Ticks每个滴答都表示 1 100 毫微秒时间间隔。Each tick in the DateTime.Ticks value represents one 100-nanosecond interval. 值中的ElapsedTicks每个计时周期表示除以Frequency1 秒的时间间隔。Each tick in the ElapsedTicks value represents the time interval equal to 1 second divided by the Frequency.

适用于

另请参阅