Codetimer 学习...

以下是全部代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
    public static class CodeTimer
    {
        public static void Initialize()
        {
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
        }
        public static void Time(String name, int interation, Action action)
        {
            ConsoleColor currentForeColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(name);
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
            Int32[] gcCounts = new Int32[GC.MaxGeneration + 1];
            for (Int32 Incremental = 0; Incremental < gcCounts.Length; Incremental++)

                gcCounts[Incremental] = GC.CollectionCount(Incremental);
            Stopwatch watch = new Stopwatch(); watch.Start();
            Int64 CycleCount = GetCycleCount();
            for (Int32 Incremental = 0; Incremental < interation; Incremental++) action();
            Int64 cpuCycles = GetCycleCount() - CycleCount;

            watch.Stop();
            Console.ForegroundColor = currentForeColor;
            Console.WriteLine("/t执行时间:/t" + watch.ElapsedMilliseconds.ToString() + "毫秒");
            Console.WriteLine("/tCPU时钟周期:/t" + cpuCycles.ToString());
            for (Int32 Incremental = 0; Incremental < GC.MaxGeneration; Incremental++)
            {
                Int32 count = GC.CollectionCount(Incremental) - gcCounts[Incremental];
                Console.WriteLine("/t" + Incremental + "代回收次数:/t" + count);
            }
        }
        private static Int64 GetCycleCount()
        {
            Int64 n, kerneltime, usertime;
            GetThreadTimes(GetCurrentThread(), out n, out n, out kerneltime, out usertime);
            return kerneltime + usertime;
        }
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool GetThreadTimes(IntPtr hThread,
                                          out long lpCreationTime,
                                          out long lpExitTime,
                                          out long lpKernelTime,
                                          out long longlpUserTime);
        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentThread();

    }
}

 

如下测试:

            CodeTimer.Initialize();

            String strTestOne = String.Empty;

            CodeTimer.Time("String Concat", 100000, () => { strTestOne += "a"; });

            StringBuilder strTestTwo = new StringBuilder(100000);

            CodeTimer.Time("StringBuilder", 100000, () => { strTestTwo.Append("a"); });

            Console.Read();

    

转载于:https://www.cnblogs.com/szjdw/archive/2011/12/09/2282626.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值