JAVA System.currentTimeMillis () or C # Environment.TickCount?

Hey, lately, I've been trying to find good ways to smooth out the threads of sleep (if bothering them, or if your computer lags, etc.).

So what's the general "best performance" method?

JAVA System.currentTimeMillis () method for C #:

public static double GetCurrentMilliseconds()
{
    DateTime staticDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    TimeSpan timeSpan = DateTime.UtcNow - staticDate;
    return timeSpan.TotalMilliseconds;
}

      

System.currentTimeMillis () is used:

    public void Run()
    {
        this.lastUpdate = TimeUtilities.GetCurrentMilliseconds();
        while (isRunning)
        {
            if (TimeUtilities.GetCurrentMilliseconds() - lastUpdate >= 600)
            {
                lastUpdate = TimeUtilities.GetCurrentMilliseconds();
                // bla bla bla...
            }

            try
            {
                Thread.Sleep(100);
            }
            catch (Exception ex)
            {
                Jolt.GetLog().WriteException(ex);
            }
        }
    }

      

and C # Environment.TickCount:

    private void Run()
    {
        double lastUpdate = Environment.TickCount;

        while (isRunning)
        {
            if (lastUpdate + 600 < Environment.TickCount)
            {
                lastUpdate = Environment.TickCount;
                // bla bla bla...
            }

            try
            {
                Thread.Sleep(100);
            }
            catch (Exception ex)
            {
                Jolt.GetLog().WriteException(ex);
            }
        }
    }

      

Any help would be greatly appreciated. Otherwise, if this is a bad idea, could you please provide a better way to do it.

+2


source to share


2 answers


Using the TickCount approach has better performance for several reasons:

  • it is an integer, not a multibyte data structure.
  • it is directly accessible without requiring various calculations


For performance, you shouldn't use a double type. To measure an interval of 100 seconds, an int must be sufficient. Note that TickCount goes from positive to negative after 24 days, so using TickCount will no longer work after that time if you use double; if you are using int you should write it as

if (Environment.TickCount-lastUpdate > 600)

      

+2


source


TickCount has the best performance.



Current time overhead is not needed in this utility. You just have to worry about the elapsed time, so it doesn't matter if the era is 1970 or the beginning of the process.

0


source







All Articles