Stopwatch.ElapsedMilliseconds プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Stopwatch.ElapsedMilliseconds プロパティの意味・解説 

Stopwatch.ElapsedMilliseconds プロパティ

メモ : このプロパティは、.NET Framework version 2.0新しく追加されたものです。

現在のインスタンス計測され経過時間合計取得します (ミリ秒単位)。

名前空間: System.Diagnostics
アセンブリ: System (system.dll 内)
構文構文

Public ReadOnly Property
 ElapsedMilliseconds As Long
Dim instance As Stopwatch
Dim value As Long

value = instance.ElapsedMilliseconds
public long ElapsedMilliseconds { get; }
public:
property long long ElapsedMilliseconds {
    long long get ();
}
/** @property */
public long get_ElapsedMilliseconds ()
public function get ElapsedMilliseconds
 () : long

プロパティ
現在のインスタンス計測されミリ秒合計を表す読み取り専用の長整数

解説解説
使用例使用例

Stopwatch クラス使用して文字列から整数解析するための 4 種類実装パフォーマンス計測する例を次に示します

   Private Shared Sub TimeOperations()

      Dim nanosecPerTick As Long
 = 1000000000 / Stopwatch.Frequency
      Const numIterations As Long
 = 10000
      
      ' Define the operation title names.
      Dim operationNames As String()
 =  _
        {"Operation: Int32.Parse(""0"")",
 _
         "Operation: Int32.TryParse(""0"")",
 _
         "Operation: Int32.Parse(""a"")",
 _
         "Operation: Int32.TryParse(""a"")"}
      
      ' Time four different implementations for parsing 
      ' an integer from a string. 

      Dim operation As Integer
      For operation = 0 To 3
         ' Define variables for operation statistics.
         Dim numTicks As Long
 = 0
         Dim numRollovers As Long
 = 0
         Dim maxTicks As Long
 = 0
         Dim minTicks As Long
 = Int64.MaxValue
         Dim indexFastest As Integer
 = - 1
         Dim indexSlowest As Integer
 = - 1
         Dim milliSec As Long
 = 0
         
         Dim time10kOperations As Stopwatch
 = Stopwatch.StartNew()
         
         ' Run the current operation 10001 times.
         ' The first execution time will be tossed
         ' out, since it can skew the average time.
         Dim i As Integer
         For i = 0 To numIterations
            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
            
            ' Skip over the time for the first operation,
            ' just in case it caused a one-time
            ' performance hit.
            If i = 0 Then
               time10kOperations.Reset()
               time10kOperations.Start()
            Else
               
               ' Update operation statistics
               ' for iterations 1-10001.
               If maxTicks < ticksThisTime Then
                  indexSlowest = i
                  maxTicks = ticksThisTime
               End If
               If minTicks > ticksThisTime Then
                  indexFastest = i
                  minTicks = ticksThisTime
               End If
               numTicks += ticksThisTime
               If numTicks < ticksThisTime Then
                  ' Keep track of rollovers.
                  numRollovers += 1
               End If
            End If
         Next i
         
         ' Display the statistics for 10000 iterations.
         time10kOperations.Stop()
         milliSec = time10kOperations.ElapsedMilliseconds
         
         Console.WriteLine()
         Console.WriteLine("{0} Summary:", operationNames(operation))
         Console.WriteLine("  Slowest time:  #{0}/{1} = {2} ticks",
 _
            indexSlowest, numIterations, maxTicks)
         Console.WriteLine("  Fastest time:  #{0}/{1} = {2} ticks",
 _
            indexFastest, numIterations, minTicks)
         Console.WriteLine("  Average time:  {0} ticks = {1} nanoseconds",
 _
            numTicks / numIterations, numTicks * nanosecPerTick / numIterations)
         Console.WriteLine("  Total time looping through {0} operations:
 {1} milliseconds", _
            numIterations, milliSec)
      Next operation

   End Sub
End Class
private static void TimeOperations()
{
    long nanosecPerTick = (1000L*1000L*1000L) / Stopwatch.Frequency;
    const long numIterations = 10000;

    // Define the operation title names.
    String [] operationNames = {"Operation: Int32.Parse(\"0\")"
,
                                   "Operation: Int32.TryParse(\"0\")"
,
                                   "Operation: Int32.Parse(\"a\")"
,
                                   "Operation: Int32.TryParse(\"a\")"};
 

    // Time four different implementations for parsing 
    // an integer from a string. 

    for (int operation = 0; operation <=
 3; operation++)
    {
        // Define variables for operation statistics.
        long numTicks = 0;
        long numRollovers = 0;
        long maxTicks = 0;
        long minTicks = Int64.MaxValue;
        int indexFastest = -1;
        int indexSlowest = -1;
        long milliSec = 0;

        Stopwatch time10kOperations = Stopwatch.StartNew();

        // Run the current operation 10001 times.
        // The first execution time will be tossed
        // out, since it can skew the average time.

        for (int i=0; i<=numIterations;
 i++) 
        {
            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;
            }

            // Skip over the time for the first operation,
            // just in case it caused a one-time
            // performance hit.
            if (i == 0)
            {
                time10kOperations.Reset();
                time10kOperations.Start();
            }
            else 
            {

                // Update operation statistics
                // for iterations 1-10001.
                if (maxTicks < ticksThisTime)
                {
                    indexSlowest = i;
                    maxTicks = ticksThisTime;
                }
                if (minTicks > ticksThisTime)
                {
                    indexFastest = i;
                    minTicks = ticksThisTime;
                }
                numTicks += ticksThisTime;
                if (numTicks < ticksThisTime)
                {
                    // Keep track of rollovers.
                    numRollovers ++;
                }
            }
        }  
        
        // Display the statistics for 10000 iterations.

        time10kOperations.Stop();
        milliSec = time10kOperations.ElapsedMilliseconds;

        Console.WriteLine();
        Console.WriteLine("{0} Summary:", operationNames[operation]);
        Console.WriteLine("  Slowest time:  #{0}/{1} = {2} ticks",
            indexSlowest, numIterations, maxTicks);
        Console.WriteLine("  Fastest time:  #{0}/{1} = {2} ticks",
            indexFastest, numIterations, minTicks);
        Console.WriteLine("  Average time:  {0} ticks = {1} nanoseconds",
 
            numTicks / numIterations, 
            (numTicks * nanosecPerTick) / numIterations );
        Console.WriteLine("  Total time looping through {0} operations: {1}
 milliseconds", 
            numIterations, milliSec);
    }
}
void TimeOperations()
{
   Int64 nanosecPerTick = (1000L * 1000L * 1000L) / Stopwatch::Frequency;
   const long numIterations = 10000;
   
   // Define the operation title names.
   array<String^>^operationNames = {"Operation: Int32.Parse(\"0\")","Operation:
 Int32.TryParse(\"0\")","Operation: Int32.Parse(\"a\")","Operation:
 Int32.TryParse(\"a\")"};
   
   // Time four different implementations for parsing 
   // an integer from a string. 
   for ( int operation = 0; operation <=
 3; operation++ )
   {
      // Define variables for operation statistics.
      Int64 numTicks = 0;
      Int64 numRollovers = 0;
      Int64 maxTicks = 0;
      Int64 minTicks = Int64::MaxValue;
      int indexFastest = -1;
      int indexSlowest = -1;
      Int64 milliSec = 0;
      Stopwatch ^ time10kOperations = Stopwatch::StartNew();
      
      // Run the current operation 10001 times.
      // The first execution time will be tossed
      // out, since it can skew the average time.
      for ( int i = 0; i <= numIterations;
 i++ )
      {
         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;
         }

         // Skip over the time for the first operation,
         // just in case it caused a one-time
         // performance hit.
         if ( i == 0 )
         {
            time10kOperations->Reset();
            time10kOperations->Start();
         }
         else
         {
            // Update operation statistics
            // for iterations 1-10001.
            if ( maxTicks < ticksThisTime )
            {
               indexSlowest = i;
               maxTicks = ticksThisTime;
            }
            if ( minTicks > ticksThisTime )
            {
               indexFastest = i;
               minTicks = ticksThisTime;
            }
            numTicks += ticksThisTime;
            if ( numTicks < ticksThisTime )
            {
               // Keep track of rollovers.
               numRollovers++;
            }
         }
      }
      
      // Display the statistics for 10000 iterations.
      time10kOperations->Stop();
      milliSec = time10kOperations->ElapsedMilliseconds;
      Console::WriteLine();
      Console::WriteLine( "{0} Summary:", operationNames[ operation ] );
      Console::WriteLine( "  Slowest time:  #{0}/{1} = {2} ticks", indexSlowest,
 numIterations, maxTicks );
      Console::WriteLine( "  Fastest time:  #{0}/{1} = {2} ticks", indexFastest,
 numIterations, minTicks );
      Console::WriteLine( "  Average time:  {0} ticks = {1} nanoseconds",
 numTicks / numIterations, (numTicks * nanosecPerTick) / numIterations );
      Console::WriteLine( "  Total time looping through {0} operations: {1}
 milliseconds", numIterations, milliSec );

   }
}
private static void TimeOperations()
{
    long nanoSecPerTick = (1000L * 1000L * 1000L) / Stopwatch.Frequency;
    final long numIterations = 10000;

    // Define the operation title names.
    String operationNames[] =  { "Operation: Int32.Parse(\"0\")",
 
        "Operation: Int32.TryParse(\"0\")", "Operation:
 Int32.Parse(\"a\")",
        "Operation: Int32.TryParse(\"a\")" };

    // Time four different implementations for parsing 
    // an integer from a string. 
    for (int operation = 0; operation <=
 3; operation++) {
        // Define variables for operation statistics.
        long numTicks = 0;
        long numRollovers = 0;
        long maxTicks = 0;
        long minTicks = Int64.MaxValue;
        int indexFastest = -1;
        int indexSlowest = -1;
        long milliSec = 0;

        Stopwatch time10kOperations = Stopwatch.StartNew();
        // Run the current operation 10001 times.
        // The first execution time will be tossed
        // out, since it can skew the average time.
        for (int i = 0; i <= numIterations;
 i++) {
            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;
            }
            // Skip over the time for the first operation,
            // just in case it caused a one-time
            // performance hit.
            if (i == 0) {
                time10kOperations.Reset();
                time10kOperations.Start();
            }
            else {
                // Update operation statistics
                // for iterations 1-10001.
                if (maxTicks < ticksThisTime) {
                    indexSlowest = i;
                    maxTicks = ticksThisTime;
                }
                if (minTicks > ticksThisTime) {
                    indexFastest = i;
                    minTicks = ticksThisTime;
                }
                numTicks += ticksThisTime;
                if (numTicks < ticksThisTime) {
                    // Keep track of rollovers.
                    numRollovers++;
                }
            }
        }
        // Display the statistics for 10000 iterations.
        time10kOperations.Stop();
        milliSec = time10kOperations.get_ElapsedMilliseconds();

        Console.WriteLine();
        Console.WriteLine("{0} Summary:", 
            operationNames.get_Item(operation));
        Console.WriteLine("  Slowest time:  #{0}/{1} = {2} ticks", 
            (Int32)indexSlowest, (Int64)numIterations, (Int64)maxTicks);
        Console.WriteLine("  Fastest time:  #{0}/{1} = {2} ticks", 
            (Int32)indexFastest, (Int64)numIterations, (Int64)minTicks);
        Console.WriteLine("  Average time:  {0} ticks = {1} nanoseconds",
 
            (System.Double)(numTicks / numIterations), 
            (System.Double)(numTicks * nanoSecPerTick / numIterations));
        Console.WriteLine("  Total time looping through {0} operations: "
            + "{1} milliseconds", (Int64)numIterations, (Int64)milliSec);
    }
} //TimeOperations 
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Stopwatch.ElapsedMilliseconds プロパティ」の関連用語

Stopwatch.ElapsedMilliseconds プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Stopwatch.ElapsedMilliseconds プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS