Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Performance of Java Stopwatch class and example analysis of time timer

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you the Java Stopwatch class performance and time timer example analysis, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

When studying performance, you can use Stopwatch timers to calculate the efficiency of a technology. But sometimes when you want to know the performance of a technology, you often can't remember that you can use Stopwatch. It's so sad.

Attributes:

Elapsed gets the total elapsed time measured by the current instance.

ElapsedMilliseconds gets the total elapsed time (in milliseconds) measured by the current instance.

ElapsedTicks gets the total elapsed time measured by the current instance (expressed in timer periods).

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 interval measurement and resets the elapsed time to zero.

Restart stops the interval measurement, resets the elapsed time to zero, and then starts measuring the elapsed time.

Start starts or continues to measure the elapsed time of an interval.

StartNew initializes the new Stopwatch instance, sets the runtime property to zero, and then starts measuring the elapsed time.

Stop stops measuring the elapsed time of a certain 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 whether the true timer is running. Sw.Stop (); / / timing end Console.WriteLine (sw.Elapsed); / / output 00sw.Elapsed 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 to get the current minimum number of time units in the timer mechanism. Sw.Reset (); / / reset Console.WriteLine (sw.Elapsed.ToString ()); / / output 00:00:00 / / returns TimeSpan type and can handle sw.Restart () at will; / / restart 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 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 the FileStream and File classes:

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 a set of random numbers List ListInt = new List (); Random r = new Random (); for (int I = 0; I < 1000000) Generate a collection of 1m digits {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 result:" + count); sw.Stop (); Console.WriteLine ("time used for traversal:" + sw.ElapsedMilliseconds); / / output 13 Stopwatch sw2 = new Stopwatch (); sw2.Start (); int count2 = 0; count2 = ListInt.Sum () Console.WriteLine ("Linq calculation result:" + count2); Console.WriteLine ("Linq usage time:" + sw2.ElapsedMilliseconds); / / output 12 sw2.Stop (); Console.ReadKey ();}}

After running 3Jing 5 times, the performance of Linq is really good. Several times are equal, some times less than 1 millisecond. Looks like Linq to OBJECT is really reliable. The efficiency and elegance are quite good, which is worthy of the control made by the master.

The above is a sample analysis of the performance and time timer of the Java Stopwatch class. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report