[Unity] 성능 비교 정리

2021. 5. 31. 18:21[Unity] 게임 개발

반응형

# 캐싱 2ms

ball.testTrigger = !ball.testTrigger;

 

# GetComponent 362ms

ballObj.GetComponent<Ball>().testTrigger = !ballObj.GetComponent<Ball>().testTrigger;

 

# GetComponent 널체크 239ms

Ball _ball = ballObj.GetComponent<Ball>();

if (_ball != null)

_ball.testTrigger = !_ball.testTrigger;

 

# TryGetComponent 널체크 202ms

if (ballObj.TryGetComponent(out Ball _ball))

_ball.testTrigger = !_ball.testTrigger;

 

# Vector3.Distance 41ms

if (Vector3.Distance(startPos, endPos) > 0.1f)

result = true;

 

# Vector3.SqrMagnitude 42ms

if (Vector3.SqrMagnitude(startPos - endPos) > 0.1f * 0.1f)

result = true;

 

# ().sqrMagnitude 40ms

if ((startPos - endPos).sqrMagnitude > 0.1f * 0.1f)

result = true;

 

# 4-5 4ms

if (value0 - value1 == -1)

result = true;

 

# 4+(-5) 4ms

if (value0 + value2 == -1)

result = true;

 

# set value 2ms

intValue = 3;

 

# set Value 7ms

IntValue = 3;

 

# get value 2ms

resultValue = intValue;

 

# get Value 7ms

resultValue = IntValue;

 

# ?. 8ms

myClass?.MyFunction();

 

# != null 8ms

if (myClass != null)

myClass.MyFunction();

 

# Array Setup 17ms

float[] a = new float[100000000];

 

# List Setup 105ms

List<float> l = new List<float>(new float[100000000]);

 

# List Setup Capacity 19ms

List<float> l = new List<float>(100000000);

 

# Lerp 171ms

ball.position = Vector3.Lerp(ball.position, endPos, Time.deltaTime);

 

# Lerp with if true일동안 195ms false일동안 96ms

if((ball.position - endPos).sqrMagnitude > 0.1f * 0.1f)

ball.position = Vector3.Lerp(ball.position, endPos, Time.deltaTime);

 

# Camera.main 90ms

Camera.main.orthographicSize = 5;

 

# Camera cache 51ms

cam.orthographicSize = 5;

 

# for 252ms

List<int> _resultList = new List<int>();

for (int j = 0; j < originIntList.Count; j++)

{

     if (originIntList[j] < 500000)

          _resultList.Add(originIntList[j]);

}

resultList = _resultList;

 

# foreach 227ms

List<int> _resultList = new List<int>();

foreach (var item in originIntList)

{

    if(item < 500000)

        _resultList.Add(item);

}

resultList = _resultList;

 

# LINQ 237ms

List<int> _resultList = new List<int>();

_resultList = originIntList.Where(x => x > 500000).ToList();

resultList = _resultList;

 

출처- 고라니 유튜브 채널

반응형