I have the following Update()
code which calculatecalculates the approximate amountnumber of looploops per second.:
// Update is called once per frame
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
void Update()
{
long loopCount = 0;
long maxLoopCount = 500000000; // five hundred million
double startingTime = Time.realtimeSinceStartupAsDouble;
while(true)
{
loopCount++;
if(loopCount >= maxLoopCount)
{
break;
}
}
double endTime = Time.realtimeSinceStartupAsDouble;
double elapsedTime = (endTime - startingTime);
Debug.Log("elapsed time: " + elapsedTime);
Debug.Log("loop count: " + loopCount);
Debug.Log("approximate looploops per second: " + (loopCount*(1/elapsedTime)));
}
Notice that iI used NoOptimizationthe NoOptimization
and NoInlining attributeNoInlining
attributes, which means the loop is not optimized away by the compiler. I checked the debug log and below are the longest elapsed timetimes logged in the console (the slowest loop performance).:
elapsed time: 0.249041300000044
loop count: 500000000
approximate looploops per second: 2007699124.60267
It can do 500 million looploops in just a quarter of a second, which means it can do approximately 2 billion looploope per second.
I have a second batch of code which also calculatecalculates the approximate amountnumber of looploops per second, but iI used Time.realtimeSinceStartupAsDouble
in the loop.:
// Update is called once per frame
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
void Update()
{
long loopCount = 0;
double startingTime = Time.realtimeSinceStartupAsDouble;
double endTime = startingTime + 1;
while (true)
{
loopCount++;
if (Time.realtimeSinceStartupAsDouble >= endTime)
{
break;
}
}
double elapsedTime = (endTime - startingTime);
Debug.Log("elapsed time: " + elapsedTime);
Debug.Log("loop count: " + loopCount);
Debug.Log("approximate looploops per second: " + (loopCount * (1 / elapsedTime)));
}
I checked the debug log and below are the slowest looploops logged in the console.:
It can only do 27 million looploops per second, in contrast to 2 billion looploops per second from earlier. The previous code is approximately 74x faster than the code with Time.realtimeSinceStartupAsDouble
. After finding this, i doI did another test with Time.realtimeSinceStartup
instead of the double version.
// Update is called once per frame
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
void Update()
{
long loopCount = 0;
float startingTime = Time.realtimeSinceStartup;
float endTime = startingTime + 1;
while (true)
{
loopCount++;
if (Time.realtimeSinceStartup >= endTime)
{
break;
}
}
float elapsedTime = (endTime - startingTime);
Debug.Log("elapsed time: " + elapsedTime);
Debug.Log("loop count: " + loopCount);
Debug.Log("approximate loop per second: " + (loopCount * (1 / elapsedTime)));
}
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
void Update()
{
long loopCount = 0;
float startingTime = Time.realtimeSinceStartup;
float endTime = startingTime + 1;
while (true)
{
loopCount++;
if (Time.realtimeSinceStartup >= endTime)
{
break;
}
}
float elapsedTime = (endTime - startingTime);
Debug.Log("elapsed time: " + elapsedTime);
Debug.Log("loop count: " + loopCount);
Debug.Log("approximate loops per second: " + (loopCount * (1 / elapsedTime)));
}
I checked the debug log and below are the slowest looploops logged in the console.
It can only do 24 million looploops per second, similar to the Time.realtimeSinceStartupAsDouble
version.
Does anyone know why it is so slow? Now i'mI'm hesitant to use it inside a loop because it can make the loop significantly slower. What are the alternativealternatives to these two that iI can use to do timing inside a loop?
My specificationspecifications:
Unity version: 2022.3.14f1
Scripting Backend: Mono
.NET version : .NET framework
Managed Code Stripping: Disabled
CPU: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80 Ghz
OS: Windows 10 Home Single Language
- Unity version: 2022.3.14f1
- Scripting Backend: Mono
- .NET version : .NET framework
- Managed Code Stripping: Disabled
- CPU: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80 Ghz
- OS: Windows 10 Home Single Language