Stopwatch.ElapsedTicks プロパティ
アセンブリ: System (system.dll 内)
構文
解説
このプロパティは、基になるタイマ機構の経過したタイマ刻みの数を表します。タイマ刻みは、Stopwatch タイマが計測できる最小の時間単位です。ElapsedTicks 値を秒数に変換するには、Frequency フィールドを使用します。
Stopwatch インスタンスの実行中または停止中に、Elapsed、ElapsedMilliseconds、および ElapsedTicks の各プロパティを問い合わせることができます。経過時間のプロパティは、Stopwatch の実行中は徐々に増加し、インスタンスの停止中は変化しません。
既定では、Stopwatch インスタンスの経過時間の値は、計測されたすべての時間間隔の合計と等しくなります。Start を呼び出すたびに、累積経過時間のカウントが開始されます。Stop を呼び出すたびに、現在の間隔計測を終了して、累積経過時間の値を固定します。既存の Stopwatch インスタンスの累積経過時間を消去するには、Reset メソッドを使用します。
メモ |
---|
Stopwatch タイマ刻みは、DateTime.Ticks とは異なります。DateTime.Ticks 値の各タイマ刻みは、100 ナノ秒間隔を表します。ElapsedTicks 値の各タイマ刻みは、1 秒を Frequency で割った値に等しい時間間隔を表します。 |
Stopwatch クラスを使用して、文字列から整数を解析するための 4 種類の実装のパフォーマンスを計測する例を次に示します。
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
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; }
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 = 0; 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 exp) { inputNum = 0; } // Stop the timer, and save the // elapsed ticks for the operation. timePerParse.Stop(); ticksThisTime = timePerParse.get_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.get_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 exp) { inputNum = 0; } // Stop the timer, and save the // elapsed ticks for the operation. timePerParse.Stop(); ticksThisTime = timePerParse.get_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.get_ElapsedTicks(); break; default: break; }
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- Stopwatch.ElapsedTicks プロパティのページへのリンク