Java Stopwatch Class Performance and Time Timer Case Explanation

  • 2021-11-10 09:40:15
  • OfStack

When studying performance, you can use Stopwatch timer to calculate the efficiency of one technology. But sometimes when you want to know the performance of a certain technology, you often can't remember to use Stopwatch, which is too sad.

Attributes:

Elapsed gets the total runtime measured by the current instance.
ElapsedMilliseconds Gets the total elapsed time (in milliseconds) measured by the current instance.
ElapsedTicks gets the total runtime measured by the current instance (expressed in timer timings).
IsRunning Gets a value indicating whether the Stopwatch timer is running.

Method

GetTimestamp Gets the current minimum number of time units in the timer mechanism.
Reset stops the time interval measurement and resets the run time to zero.
Restart stops the interval measurement, resets the elapsed time to zero, and then starts measuring the elapsed time.
Start begins or continues to measure the elapsed time of a time interval.
StartNew initializes the new Stopwatch instance, sets the runtime property to zero, and then starts measuring the runtime.
Stop stops measuring the elapsed time of a time interval.

Example:


static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();     // Start timing 
            WebClient wc = new WebClient();
            string str = wc.DownloadString("http://www.jmeii.com/");
            Console.WriteLine(sw.IsRunning);    // Output  true    Whether the timer is running. 
            sw.Stop();      // End of timing 
            Console.WriteLine(sw.Elapsed);      // Output  00:00:00.3452873
            Console.WriteLine(sw.ElapsedMilliseconds);  // Output  223
            Console.WriteLine(sw.ElapsedTicks); // Output 501838
            Console.WriteLine(sw.IsRunning);    // Output  flase

            Console.WriteLine(Stopwatch.GetTimestamp());   // Output 56151531319   Gets the current minimum number of time units in the timer mechanism. 

            sw.Reset(); // Reset 
            Console.WriteLine(sw.Elapsed.ToString());   // Output  00:00:00   // Returns the TimeSpan Type, you can handle it at will 

            sw.Restart();   // Start over 
            FileStream fs = new FileStream(@"D:\admin_b.txt", FileMode.Open, FileAccess.Read);
            //string str = 
            byte[] byteArr = new byte[fs.Length];
            fs.Read(byteArr, 0, (int)fs.Length);
            Stopwatch sw2 = Stopwatch.StartNew();       // Create 1 A new timer 
            string str2 = Encoding.UTF8.GetString(byteArr);
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);  // Output  10
            Console.WriteLine(sw2.ElapsedMilliseconds); // Output  9
            sw2.Stop();
            
            Console.ReadKey();
        }

Compare FileStream with File:


static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            FileStream fs = new FileStream(@"D:\admin_b.txt", FileMode.Open, FileAccess.Read);
            //string str = 
            byte[] byteArr = new byte[fs.Length];
            fs.Read(byteArr, 0, (int)fs.Length);
            string str = Encoding.UTF8.GetString(byteArr);
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);  // Output  12

            Stopwatch sw2 = new Stopwatch();
            sw2.Start();
            File.ReadAllText(@"E:\admin_b.txt");
            sw2.Stop();
            Console.WriteLine(sw2.ElapsedMilliseconds); // Output  11

            Console.ReadKey();
        }

Linq example:


public class Program
    {
        static void Main(string[] args)
        {
            // Get to 1 Set of random numbers 
            List<int> ListInt = new List<int>();
            Random r = new Random();
            for (int i = 0; i < 1000000; i++)       // Generate 1 A 1 A collection of millions of numbers 
            {
                ListInt.Add(r.Next(1000));
            }

            Stopwatch sw = new Stopwatch();
            sw.Start();
            int count = 0;
            foreach (var i in ListInt)
            {
                count = count + i;          
            }
            Console.WriteLine(" Traversal calculation results: " + count);       
            sw.Stop();
            Console.WriteLine(" Traversal time: " + sw.ElapsedMilliseconds);    // Output  13

            Stopwatch sw2 = new Stopwatch();
            sw2.Start();
            int count2 = 0;
            count2 = ListInt.Sum();
            Console.WriteLine("Linq The calculation result of: " + count2);
            Console.WriteLine("Linq Use time of: " + sw2.ElapsedMilliseconds);  // Output  12
            sw2.Stop();

            Console.ReadKey();
        }
    }

After running 3 or 5 times, the performance of Linq is really amazing. Several times it was equal, and several times it was less than 1 millisecond. It seems that Linq to OBJECT is really reliable. Efficiency and elegance are quite good, which is worthy of being a master control. In the future, if you have any questions about what technology, you can use this class to test, remember, remember, remember.


Related articles: