Unity3D之获取某个方法执行的时间.docx
《Unity3D之获取某个方法执行的时间.docx》由会员分享,可在线阅读,更多相关《Unity3D之获取某个方法执行的时间.docx(4页珍藏版)》请在冰豆网上搜索。
Unity3D之获取某个方法执行的时间
【Unity3D之获取某个方法执行的时间】
#region计算代码执行花费的时间
//如果要用纳秒级或微秒级的话就多测几次取平均#region纳秒级10-9秒
publicstaticvoidcalculate_cudetime_namiao(thisMonoBehaviourmono,stringmethodname)
{
Typet=mono.GetType();MethodInfovMethodInfo=t.GetMethod(methodname);
if(vMethodInfo==null)return;PerformanceTimertimer=newPerformanceTimer();
timer.Start();
vMethodInfo.Invoke(mono,null);
timer.Stop();MyDebug.Log(methodname+"使用时间纳秒"+timer.Duration*1000000000);
}publicstaticvoidcalculate_cudetime_namiao(thisMonoBehaviourmono,stringmethodname,objectpram1)
{
Typet=mono.GetType();MethodInfovMethodInfo=t.GetMethod(methodname,newType[]{pram1.GetType()});
if(vMethodInfo==null)return;PerformanceTimertimer=newPerformanceTimer();
timer.Start();
vMethodInfo.Invoke(mono,newobject[]{pram1});
timer.Stop();MyDebug.Log(methodname+"使用时间纳秒"+timer.Duration*1000000000);
}publicstaticvoidcalculate_cudetime_namiao(thisMonoBehaviourmono,stringmethodname,objectpram1,objectpram2)
{
Typet=mono.GetType();
Typep1=pram1.GetType();
Typep2=pram2.GetType();
MethodInfovMethodInfo=t.GetMethod(methodname,newType[]{pram1.GetType(),pram2.GetType()});
if(vMethodInfo==null)return;
PerformanceTimertimer=newPerformanceTimer();
timer.Start();
vMethodInfo.Invoke(mono,newobject[]{pram1,pram2});
timer.Stop();MyDebug.Log(methodname+"使用时间纳秒"+timer.Duration*1000000000);
}#endregion纳秒级10-9秒#region微秒级10-6秒
publicstaticvoidcalculate_cudetime_weimiao(thisMonoBehaviourmono,stringmethodname)
{
Typet=mono.GetType();MethodInfovMethodInfo=t.GetMethod(methodname);
if(vMethodInfo==null)return;PerformanceTimertimer=newPerformanceTimer();
timer.Start();
vMethodInfo.Invoke(mono,null);
timer.Stop();MyDebug.Log(methodname+"使用时间微秒"+timer.Duration*1000000);
}publicstaticvoidcalculate_cudetime_weimiao(thisMonoBehaviourmono,stringmethodname,objectpram1)
{
Typet=mono.GetType();MethodInfovMethodInfo=t.GetMethod(methodname,newType[]{pram1.GetType()});
if(vMethodInfo==null)return;PerformanceTimertimer=newPerformanceTimer();
timer.Start();
vMethodInfo.Invoke(mono,newobject[]{pram1});
timer.Stop();MyDebug.Log(methodname+"使用时间微秒"+timer.Duration*1000000);
}publicstaticvoidcalculate_cudetime_weimiao(thisMonoBehaviourmono,stringmethodname,objectpram1,objectpram2)
{
Typet=mono.GetType();
Typep1=pram1.GetType();
Typep2=pram2.GetType();
MethodInfovMethodInfo=t.GetMethod(methodname,newType[]{pram1.GetType(),pram2.GetType()});
if(vMethodInfo==null)return;
PerformanceTimertimer=newPerformanceTimer();
timer.Start();
vMethodInfo.Invoke(mono,newobject[]{pram1,pram2});
timer.Stop();MyDebug.Log(methodname+"使用时间微秒"+timer.Duration*1000000);
}#endregion微秒级10-6秒#region毫秒级10-3秒publicstaticvoidcalculate_cudetime(thisMonoBehaviourmono,stringmethodname)
{
Typet=mono.GetType();MethodInfovMethodInfo=t.GetMethod(methodname);
if(vMethodInfo==null)return;
StopwatchstopWatch=newStopwatch();
stopWatch.Start();
vMethodInfo.Invoke(mono,null);
stopWatch.Stop();MyDebug.Log(methodname+""+stopWatch.ElapsedMilliseconds);
}publicstaticvoidcalculate_cudetime(thisMonoBehaviourmono,stringmethodname,objectpram1)
{
Typet=mono.GetType();MethodInfovMethodInfo=t.GetMethod(methodname,newType[]{pram1.GetType()});
if(vMethodInfo==null)return;
StopwatchstopWatch=newStopwatch();
stopWatch.Start();
vMethodInfo.Invoke(mono,newobject[]{pram1});
stopWatch.Stop();MyDebug.Log(methodname+""+stopWatch.ElapsedMilliseconds);
}publicstaticvoidcalculate_cudetime(thisMonoBehaviourmono,stringmethodname,objectpram1,objectpram2)
{
Typet=mono.GetType();
Typep1=pram1.GetType();
Typep2=pram2.GetType();
MethodInfovMethodInfo=t.GetMethod(methodname,newType[]{pram1.GetType(),pram2.GetType()});
if(vMethodInfo==null)return;
StopwatchstopWatch=newStopwatch();
stopWatch.Start();
vMethodInfo.Invoke(mono,newobject[]{pram1,pram2});
stopWatch.Stop();MyDebug.Log(methodname+""+stopWatch.ElapsedMilliseconds);
}/*
调用事例必须是公有方法
*
publicvoidfun(inta)
*{
}
*update()
*{
if(Input.GetKeyDown(KeyCode.Space))
{
//calculate("Call");this.calculate_cudetime("fun",10)
}
*}*/#endregion毫秒级10-3秒#endregion计算代码执行花费的时间