Stopwatch.StartNew 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化新的 Stopwatch 執行個體,將已耗用時間屬性設定為零,然後開始測量已耗用時間。
public:
static System::Diagnostics::Stopwatch ^ StartNew();
public static System.Diagnostics.Stopwatch StartNew ();
static member StartNew : unit -> System.Diagnostics.Stopwatch
Public Shared Function StartNew () As Stopwatch
傳回
剛開始測量已耗用時間的 Stopwatch。
範例
下列範例會 Stopwatch 使用 類別來測量四個不同實作的效能,以剖析字串中的整數。 此程式代碼範例是針對 類別提供的較大範例的 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 構函式,然後在新的 實體上呼叫 Start 。