it leader

unity에서 시간 측정하는 방법

 

 

1. Stopwatch를 이용한 측정 

 

using System.diagnotics;

using debug = UnityEngine.Debug;

 

Stopwatch sw = new Stopwatch();

 

sw.Start();

sw.Stop();

sw.Reset();

 

메서드를 이용하여 시간을 측정할 수 있다.

 

내부적으로 타이머가 돌아가기 때문에 확인을 하려면 로그를 찍어봐야 된다.

 

 

Debug.Log(" sw    :  "+ sw.ElapsedMilliseconds.ToString() +"ms");

 

이렇게 로그를 넣어주면 몇 ms가 걸렸는지 확인 할 수 있다.

 

단!!!!!  diagnotics를 사용하기 때문에 UnityEngine.debug와 System.diagnotics.debug 사이에 충돌이 일어날 수 있다.

 

필수로 using debug = UnityEngine.Debug;

입력해주어야 한다.

 

 

 

2. timer 변수를 이용한 측정

 

timer변수를 매프레임 1씩증가시키는 방법이다.

 

다만 디바이스의 성능에 따라 퍼포먼스의 성능차이 가 있으므로

 

Time.daltaTime을 대입해 준다.

 

seconds = timer 

 

float timer;
float fps;

update()
{
	timer += Time.deltaTime;
	Debug.Log(" timer    : "+ timer / fps + " seconds");
}


// 화면에 프레임 레이트를 표시해 주는 함수
OnGUI()
{
	int w = Screen.width, h = Screen.height;
    GUIStyle style = new GUIStyle();
      
    Rect rect = new Rect(0, 0, w, h * 2 / 100);
    style.alignment = TextAnchor.UpperLeft;
    style.fontSize = h * 2 / 100;
    style.normal.textColor = new Color (0.0f, 0.0f, 0.5f, 1.0f);
    float msec = deltaTime * 1000.0f;
    fps = 1.0f / deltaTime;
    string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
    GUI.Label(rect, text, style);
}

 

 

두가지 방법을 알아보았는데..

간단한 씬에서는 차이가 없이 둘다 동일한 결과를 보여준다.

좀더 하드한 테스트 환경에서 테스트 해봐야지만 성능을 판단할 수 있을 것 같다.

 

이상 끝!!

 

 

 

 

profile

it leader

@dev__pixui

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!