I have the following Update()
code which calculates the approximate number of loops per second:
[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 loops per second: " + (loopCount*(1/elapsedTime)));
}
Notice that I used the NoOptimization
and NoInlining
attributes, which means the loop is not optimized away by the compiler. I checked the debug log and below are the longest elapsed times logged in the console (the slowest loop performance):
elapsed time: 0.249041300000044
loop count: 500000000
approximate loops per second: 2007699124.60267
It can do 500 million loops in just a quarter of a second, which means it can do approximately 2 billion loope per second.
I have a second batch of code which also calculates the approximate number of loops per second, but I used Time.realtimeSinceStartupAsDouble
in the loop:
[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 loops per second: " + (loopCount * (1 / elapsedTime)));
}
I checked the debug log and below are the slowest loops logged in the console:
elapsed time: 1
loop count: 27078590
approximate loop per second: 27078590
It can only do 27 million loops per second, in contrast to 2 billion loops per second from earlier. The previous code is approximately 74x faster than the code with Time.realtimeSinceStartupAsDouble
. After finding this, I did another test with Time.realtimeSinceStartup
instead of the double version.
[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 loops logged in the console.
elapsed time: 1
loop count: 24932097
approximate loop per second: 2.49321E+07
It can only do 24 million loops per second, similar to the Time.realtimeSinceStartupAsDouble
version.
Does anyone know why it is so slow? Now I'm hesitant to use it inside a loop because it can make the loop significantly slower. What are the alternatives to these two that I can use to do timing inside a loop?
My specifications:
- 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