Unity3D 简易计时类

本文介绍了一个简易计时类的实现方法,该类可用于记录代码段的执行时间,并提供了单次和总耗时的统计。通过示例展示了如何在代码中使用此计时类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>();
//  }
//}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值