好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java Stopwatch类,性能与时间计时器案例详解

  在研究性能的时候,完全可以使用Stopwatch计时器计算一项技术的效率。但是有时想知道某想技术的性能的时候,又常常想不起可以运用Stopwatch这个东西,太可悲了。

属性:

Elapsed       获取当前实例测量得出的总运行时间。
ElapsedMilliseconds  获取当前实例测量得出的总运行时间(以毫秒为单位)。
ElapsedTicks      获取当前实例测量得出的总运行时间(用计时器计时周期表示)。
IsRunning        获取一个指示 Stopwatch 计时器是否在运行的值。

方法

GetTimestamp   获取计时器机制中的当前最小时间单位数。
Reset        停止时间间隔测量,并将运行时间重置为零。
Restart       停止时间间隔测量,将运行时间重置为零,然后开始测量运行时间。
Start         开始或继续测量某个时间间隔的运行时间。
StartNew       对新的 Stopwatch 实例进行初始化,将运行时间属性设置为零,然后开始测量运行时间。
Stop         停止测量某个时间间隔的运行时间。

示例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

static void Main(string[] args)

         {

             Stopwatch sw = new Stopwatch();

             sw.Start();     //开始计时

             WebClient wc = new WebClient();

             string str = wc.DownloadString( "http://www.jmeii.com/" );

             Console.WriteLine(sw.IsRunning);    //输出 true   计时器是否在运行。

             sw.Stop();      //计时结束

             Console.WriteLine(sw.Elapsed);      //输出 0.3452873

             Console.WriteLine(sw.ElapsedMilliseconds);  //输出 223

             Console.WriteLine(sw.ElapsedTicks); //输出501838

             Console.WriteLine(sw.IsRunning);    //输出 flase

 

             Console.WriteLine(Stopwatch.GetTimestamp());   //输出56151531319  获取计时器机制中的当前最小时间单位数。

 

             sw.Reset(); //重置

             Console.WriteLine(sw.Elapsed.ToString());   //输出 0   //返回的是TimeSpan类型,可以任意处理

 

             sw.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();       //创建一个新的计时器

             string str2 = Encoding.UTF8.GetString(byteArr);

             sw.Stop();

             Console.WriteLine(sw.ElapsedMilliseconds);  //输出 10

             Console.WriteLine(sw2.ElapsedMilliseconds); //输出 9

             sw2.Stop();

            

             Console.ReadKey();

         }

  对比FileStream与File类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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);  //输出 12

 

             Stopwatch sw2 = new Stopwatch();

             sw2.Start();

             File.ReadAllText(@ "E:\admin_b.txt" );

             sw2.Stop();

             Console.WriteLine(sw2.ElapsedMilliseconds); //输出 11

 

             Console.ReadKey();

         }

  Linq示例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

public class Program

     {

         static void Main(string[] args)

         {

             //获取到一个随机数集合

             List< int > ListInt = new List< int >();

             Random r = new Random();

             for ( int i = 0 ; i < 1000000 ; i++)       //生成一个1百万数字的集合

             {

                 ListInt.Add(r.Next( 1000 ));

             }

 

             Stopwatch sw = new Stopwatch();

             sw.Start();

             int count = 0 ;

             foreach (var i in ListInt)

             {

                 count = count + i;         

             }

             Console.WriteLine( "遍历计算结果:" + count);      

             sw.Stop();

             Console.WriteLine( "遍历用时:" + sw.ElapsedMilliseconds);    //输出 13

 

             Stopwatch sw2 = new Stopwatch();

             sw2.Start();

             int count2 = 0 ;

             count2 = ListInt.Sum();

             Console.WriteLine( "Linq的计算结果:" + count2);

             Console.WriteLine( "Linq的使用时间:" + sw2.ElapsedMilliseconds);  //输出 12

             sw2.Stop();

 

             Console.ReadKey();

         }

     }

  运行了3,5次,Linq的性能的确厉害。有几次相等,有几次少1毫秒。看来Linq to OBJECT还真是靠谱。效率,优雅度都相当好,不愧是高手做的控件。以后如果对什么技术又疑问,完全可以用该类来测试,记得,记得,记得得。

到此这篇关于Java Stopwatch类,性能与时间计时器案例详解的文章就介绍到这了,更多相关Java Stopwatch类,性能与时间计时器内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/ajwqb06628/article/details/102070972

查看更多关于Java Stopwatch类,性能与时间计时器案例详解的详细内容...

  阅读:24次