Stopwatch.ElapsedTicks 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得目前實例所測量的總經過時間,以計時器tick數計算。
public:
property long ElapsedTicks { long get(); };
public long ElapsedTicks { get; }
member this.ElapsedTicks : int64
Public ReadOnly Property ElapsedTicks As Long
屬性值
一個只讀的長整數,代表目前實例測量的計時器跳躍總數。
範例
以下範例利用該 Stopwatch 類別來衡量四種不同實作在解析字串中整數的效能。 此程式碼範例是本類別更大範例 Stopwatch 的一部分。
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、、 ElapsedMilliseconds以及 ElapsedTicks 當 Stopwatch 實例執行或停止時。 當實 Stopwatch 例執行時,經過時間的特性會逐漸增加;當實例停止時,這些特性保持不變。
預設情況下,實例經過的時間值 Stopwatch 等於所有測量時間區間的總和。 每次呼叫 從 Start 累積經過時間開始計數;每次呼叫 Stop 結束當前區間測量,並凍結累積經過時間值。 使用該 Reset 方法清除現有 Stopwatch 實例的累積經過時間。
Note
Stopwatch tick 與 DateTime.Ticks不同。 該數值中的 DateTime.Ticks 每一刻都代表一個 100 奈秒的間隔。 該值中的ElapsedTicks每一刻都代表時間區間,等於 1 秒除以 。Frequency