简体   繁体   English

C#方法第一次运行比第一次运行慢

[英]c# method first run is slower than after first run

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? 第一次调用Test1()和Test2()时,为什么Update()上的CPU时间会打ic?

There's probably some JIT (Just In Time) compilation happening during the first call of the functions. 在第一次调用函数期间,可能会发生一些JIT(Just In Time)编译。

The second and subsequent times the functions are called, there's an optimized version of them waiting. 第二次及之后的函数调用,有一个优化的版本等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#-为什么此循环的第一次迭代比其他循环慢? - C# - Why does the first iteration of this loop run slower than the rest? c#winforms:确定第一次运行程序 - c# winforms: determine first run of program 如何加快C#程序在首次运行时的运行速度? - How to speed up a C# program to run quickly in the first run? 如何使方法或事件在C#程序的首次启动时运行? - How do I make a method or event run on the first startup of a C# Program? 如何安排C#Windows服务在每月的第一天运行方法? - How to schedule a C# Windows Service to run a method on the first day of the month? VS 2015在第一次运行后一直锁定我的C#Win Form Application文件 - VS 2015 keeps locking my C# Win Form Application files after first run C# 如果我一个接一个地运行 2 个异步命令,我能保证第一个总是在第二个之前运行吗? - C# If I run 2 async commands one after the other, can I guarantee the first will always run before the second? 如何在C#中的第一个和第二个方法之后执行第三个方法 - how to execute a third method after first and second method in c# 首次运行时,C#编写设置类应用程序作用域 - C# write settings class application scope on first run 我们可以强制c#编译器先运行构造函数吗? - Can we force a c# compiler to run constructor first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM