iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java Stopwatch类,性能与时间计时器案例详解
  • 537
分享到

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

2024-04-02 19:04:59 537人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

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

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

属性:

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

方法

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

示例:


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);      //输出 00:00:00.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());   //输出 00:00:00   //返回的是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类:


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示例:


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类,性能与时间计时器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java Stopwatch类,性能与时间计时器案例详解

本文链接: https://www.lsjlt.com/news/135190.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作