Stopwatch的使用

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Stopwatch的使用
 8 {
 9     class Program
10     {
11       
12         /// <summary>
13         /// 使用Stopwatch比较for循环和foreach循环的效率
14         /// </summary>
15         /// <param name="args"></param>
16         static void Main(string[] args)
17         {
18 
19             int[] intArr = new int[1000000];
20             for (int i = 1; i <= 1000000; i++)
21             {
22                 intArr[i - 1] = i;
23             }
24 
25             //使用Stopwatch统计程序运行的时间
26             /*
27              Stopwatch提供了几个方法用以控制Stopwatch对象。
28              * Start方法开始一个计时操作,Stop方法停止计时。
29              * 此时如果第二次使用 Start方法,将继续计时,最终的计时结果为两次计时的累加。
30              * 为避免这种情况,在第二次计时前用Reset方法将对象归零。这三个方法都不需要参数
31              */
32             System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); 
33             sw.Start(); //开始计时
34 
35             int sum = 0;
36 
37             for (int i = 1; i <= intArr.Length; i++)
38             {
39                 sum += i;
40             }
41             Console.WriteLine("sum={0}",sum);
42            
43             sw.Stop(); //停止计时
44             long result = sw.ElapsedMilliseconds;//获得程序运行的时间
45             Console.WriteLine("使用for循环计算从1加到1000000的和所需要的时间是:{0}", result);
46 
47             Console.WriteLine("****************************************************************");
48             sw.Reset();
49             sw.Start();
50 
51             sum = 0;
52             foreach (var item in intArr)
53             {
54                 sum += item;
55             }
56             Console.WriteLine("sum={0}", sum);
57             Console.WriteLine("使用foreach循环计算从1加到1000000的和所需要的时间是:{0}", sw.ElapsedMilliseconds);
58             sw.Stop();
59 
60             Console.ReadKey();
61         }
62     }
63 }

 

转载于:https://my.oschina.net/u/2600747/blog/616191

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值