Params性能探索
作者:互联网
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling;
namespace MyTest
{
public class TestParamsObject : MonoBehaviour
{
public int TestCount = 10000;
void Start()
{
}
void Update()
{
Profiler.BeginSample("CastInt");
for (int i = 0; i < TestCount; i++)
CastInt(1);
Profiler.EndSample();
Profiler.BeginSample("CastString");
for (int i = 0; i < TestCount; i++)
CastString("a");
Profiler.EndSample();
Profiler.BeginSample("CastObject");
for (int i = 0; i < TestCount; i++)
CastObject(this);
Profiler.EndSample();
Profiler.BeginSample("NoCastInt");
for (int i = 0; i < TestCount; i++)
NoCastInt(1);
Profiler.EndSample();
Profiler.BeginSample("NoCastString");
for (int i = 0; i < TestCount; i++)
NoCastString("a");
Profiler.EndSample();
Profiler.BeginSample("NoCastObject");
for (int i = 0; i < TestCount; i++)
NoCastObject(this);
Profiler.EndSample();
}
void CastInt(params object[] args)
{
int a = (int)args[0];
}
void CastString(params object[] args)
{
string b = (string)args[0];
}
void CastObject(params object[] args)
{
TestParamsObject c = (TestParamsObject)args[0];
}
void NoCastInt(int arg)
{
int a = arg;
}
void NoCastString(string arg)
{
string a = arg;
}
void NoCastObject(TestParamsObject arg)
{
TestParamsObject a = arg;
}
}
}
params虽然好用,但是GC很高啊,每帧10W次调用,就有数M的GC,可怕,性能也堪忧, 相对于正常的传值和传引用,执行时间大概是20倍到40倍的差距,还是少用params吧。
而且int的类型转换GC高,执行时间长,盲猜是因为装箱拆箱的问题导致的。
标签:探索,int,性能,Profiler,TestCount,Params,BeginSample,arg,void 来源: https://blog.csdn.net/xp406181/article/details/121788228