Stopwatch.StartNew メソッド
アセンブリ: System (system.dll 内)
構文
Dim returnValue As Stopwatch returnValue = Stopwatch.StartNew
経過時間の計測を開始した Stopwatch。
使用例
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.StartNew メソッドのページへのリンク