1

I made this code

using UnityEngine;

public class InstanceTest : MonoBehaviour { 
public int cnt; public bool startTest1 = false; public bool startTest2 = false;

 void Update()
 {
     if (startTest1)
         Test1(cnt);
     if (startTest2)
         Test2(cnt);
 }
 void Test1(int cnt)
 {
     float a;
     for (int i = 0; i < cnt; i++)        
         a = 1f + 1f;        
 }
 static void Test2(int cnt)
 {
     float a;
     for (int i = 0; i < cnt; i++)        
         a = 1f + 1f;        
 }
}

On Profiler

enter image description here

why CPU Time on Update() hiccup when call Test1() and Test2() at first time?

artem
  • 46,476
  • 8
  • 74
  • 78
  • What makes you think it is slower the first time? – mjwills Jul 19 '18 at 07:28
  • 2
    Possible duplicate of [Why subsequent direct method call is much faster than the first call?](https://stackoverflow.com/questions/6417598/why-subsequent-direct-method-call-is-much-faster-than-the-first-call) – mjwills Jul 19 '18 at 07:29
  • @mjwills I used RuntimeHelpers.PrepareMethod() like link's answer but still have hiccup... – min kyu Park Jul 19 '18 at 07:38

1 Answers1

2

There's probably some JIT (Just In Time) compilation happening during the first call of the functions.

The second and subsequent times the functions are called, there's an optimized version of them waiting.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • 1
    @minkyuPark You may find https://unity3d.com/learn/tutorials/topics/performance-optimization/optimizing-scripts-unity-games of interest. – mjwills Jul 19 '18 at 07:38