Unity3D 简易计时类

来源:互联网 发布:淘宝卖家蚂蚁花呗支付 编辑:程序博客网 时间:2024/05/17 05:16
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
/// <summary>
/// /// 简易的计时类
/// /// </summary>
public class YiWatch : IDisposable
{
    #region 字段
    private string testName;
    private int testCount;
    private Stopwatch watch;
    #endregion

    #region 构造函数

    public YiWatch(string name, int count)
    {
        this.testName = name; this.testCount = count > 0 ? count : 1;
        this.watch = Stopwatch.StartNew();
    }
    #endregion

    #region 方法
    public void Dispose()
    {
        this.watch.Stop();
        float totalTime = this.watch.ElapsedMilliseconds;
        UnityEngine.Debug.Log(string.Format("测试名称:{0}   总耗时:{1}   单次耗时:{2}    测试数量:{3}", this.testName, totalTime, totalTime / this.testCount, this.testCount));
    }

    #endregion
}

/*
 *使用方式
 */
//int testCount = 1000000;
////定义测试的次数
//using (new YiWatch("GetComponent<>", testCount))
//{
//  for(int i = 0; i < testCount; i++)
//  {
//    GetComponent<TestComp>();
//  }
//}
原创粉丝点击