Skip to main content
Cleanup
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

Why are Time.realtimeSinceStartup and Time.realtimeSinceStartupAsDouble is so slow when called repeatedly?

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

Why Time.realtimeSinceStartup and Time.realtimeSinceStartupAsDouble is so slow when called repeatedly?

I have the following Update() code which calculate the approximate amount of loop 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 loop per second: " + (loopCount*(1/elapsedTime)));
}

Notice that i used NoOptimization and NoInlining attribute, which means the loop is not optimized away by the compiler. I checked the debug log and below are the longest elapsed time logged in the console (the slowest loop performance).

elapsed time: 0.249041300000044
loop count: 500000000
approximate loop per second: 2007699124.60267

It can do 500 million loop in just a quarter of a second, which means it can do approximately 2 billion loop per second.

I have a second code which also calculate the approximate amount of loop per second, but i 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 loop per second: " + (loopCount * (1 / elapsedTime)));
}

I checked the debug log and below are the slowest loop logged in the console.

It can only do 27 million loop per second, in contrast to 2 billion loop per second from earlier. The previous code is approximately 74x faster than the code with Time.realtimeSinceStartupAsDouble. After finding this, i do 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)));
}

I checked the debug log and below are the slowest loop logged in the console.

It can only do 24 million loop 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 alternative to these two that i can use to do timing inside a loop?

My specification:

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

Why are Time.realtimeSinceStartup and Time.realtimeSinceStartupAsDouble so slow when called repeatedly?

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:

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.

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
added 37 characters in body
Source Link

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

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

added 81 characters in body
Source Link

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 alternative to these two that i can use to do timing inside a loop?

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.

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 alternative to these two that i can use to do timing inside a loop?

added 222 characters in body
Source Link
Loading
Source Link
Loading