Time.time vs. performance of DateTime.UtcNow.Millisecond in Unity3D?

I need to constantly keep track of the time in my multiplayer game built with Unity to find out when the data packet arrived. What would improve performance in Unity, Time.time or DateTime.UtcNow.Millisecond? And how much will the performance difference be different?

+4
source share
1 answer

It’s better to check it out yourself. You are the one who knows your application best and can say what is the most optimal solution in special conditions. This is a simple test script that you can use. It simply saves the current timestamp using the unit / C # method and the registration time required for this operation.

public class TimeTest : MonoBehaviour
{
    public bool UseUnityFuntion;

    private string msg;

    void Update ()
    {
        var stopwatch = Stopwatch.StartNew();
        msg = UseUnityFuntion 
            ? Time.time.ToString() 
            : DateTime.UtcNow.Millisecond.ToString();
        stopwatch.Stop();
        UnityEngine.Debug.Log(stopwatch.ElapsedTicks);
    }

    void OnGUI()
    {
        if (msg != null) GUILayout.Label(msg);
    }
}

As in most cases, this non-unity function seems faster. At least on my computer. I don’t know how the application works, but keep in mind that you may encounter when using the Miliseconds value. There is also one drawback to using Time.time - you can only do this in the main thread. When you have a network application, there is a good chance that you want to save the timestamp in the background thread.

+2

Source: https://habr.com/ru/post/1672124/


All Articles