Stopwatch.ElapsedTicks 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取当前实例以计时器计时周期为单位测量的总已用时间。
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 实例中的累积已用时间。
注释
Stopwatch 滴答声不同于 DateTime.Ticks。 值 DateTime.Ticks 中的每个刻度表示一个 100 纳秒间隔。 值 ElapsedTicks 中的每个刻度表示时间间隔等于 1 秒除以 Frequency。