Stopwatch.ElapsedTicks プロパティ

定義

現在のインスタンスで計測された経過時間の合計を取得します (タイマー刻み)。

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

プロパティ値

現在のインスタンスで計測されたタイマー刻みの合計を表す読み取り専用の長整数。

次の例では、 クラスを Stopwatch 使用して、文字列から整数を解析するための 4 つの異なる実装のパフォーマンスを測定します。 このコード例は、Stopwatch クラスのために提供されている大規模な例の一部です。

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

注釈

このプロパティは、基になるタイマー メカニズムの経過ティック数を表します。 ティックは、タイマーが測定できる最小の時間 Stopwatch 単位です。 フィールドを Frequency 使用して、値を ElapsedTicks 秒数に変換します。

インスタンスの実行中または停止中に、プロパティ Elapsed、、および ElapsedTicks に対してStopwatchクエリElapsedMillisecondsを実行できます。 経過時間プロパティは、 の実行中に着実に増加 Stopwatch し、インスタンスが停止しても一定のままです。

既定では、インスタンスの経過時間値は Stopwatch 、測定されたすべての時間間隔の合計と等しくなります。 の Start 各呼び出しは累積経過時間でカウントを開始します。を Stop 呼び出すたびに、現在の間隔の測定が終了し、累積経過時間の値が固定されます。 メソッドを Reset 使用して、既存 Stopwatch のインスタンスの累積経過時間をクリアします。

注意

Stopwatch ティックは とは異なります DateTime.Ticks。 値の各ティックは DateTime.Ticks 、100 ナノ秒間隔を表します。 値の ElapsedTicks 各ティックは、1 秒に等しい時間間隔を で除算して表します Frequency

適用対象

こちらもご覧ください