Stopwatch.ElapsedTicks Egenskap
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar den totala förflutna tiden som mäts av den aktuella instansen, i tidsinställda tick.
public:
property long ElapsedTicks { long get(); };
public long ElapsedTicks { get; }
member this.ElapsedTicks : int64
Public ReadOnly Property ElapsedTicks As Long
Egenskapsvärde
Ett skrivskyddat långt heltal som representerar det totala antalet tidsinställda tick som mäts av den aktuella instansen.
Exempel
I följande exempel används Stopwatch klassen för att mäta prestanda för fyra olika implementeringar för att parsa ett heltal från en sträng. Det här kodexemplet är en del av ett större exempel för Stopwatch klassen.
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
Kommentarer
Den här egenskapen representerar antalet förflutna tick i den underliggande timermekanismen. En bock är den minsta tidsenhet som Stopwatch timern kan mäta. Använd fältet Frequency för att konvertera värdet ElapsedTicks till ett antal sekunder.
Du kan fråga egenskaperna Elapsed, ElapsedMillisecondsoch ElapsedTicks medan instansen Stopwatch körs eller stoppas. De förflutna tidsegenskaperna ökar stadigt medan Stopwatch körs. De förblir konstanta när instansen stoppas.
Som standard är det förflutna tidsvärdet för en Stopwatch instans lika med summan av alla uppmätta tidsintervall. Varje anrop till Start börjar räkna vid den kumulativa förflutna tiden. Varje anrop avslutar Stop den aktuella intervallmätningen och fryser det kumulativa förflutna tidsvärdet. Reset Använd metoden för att rensa den kumulativa förflutna tiden i en befintlig Stopwatch instans.
Note
Stopwatch fästingar skiljer sig från DateTime.Ticks. Varje tick i värdet DateTime.Ticks representerar ett intervall på 100 nanosekunder. Varje tick i värdet ElapsedTicks representerar tidsintervallet som är lika med 1 sekund dividerat med Frequency.