【问题标题】:c# method first run is slower than after first runc#方法第一次运行比第一次运行慢
【发布时间】:2018-08-09 10:04:04
【问题描述】:

我编写了这段代码

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;        
 }
}

在探查器上

enter image description here

为什么在第一次调用 Test1() 和 Test2() 时 CPU Time on Update() 打嗝?

【问题讨论】:

标签: c# unity3d


【解决方案1】:

在第一次调用函数期间可能会发生一些 JIT(即时)编译。

第二次及后续调用函数时,等待它们的优化版本。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
  • 2021-11-13
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
  • 2020-01-29
相关资源
最近更新 更多