diff --git a/Projects/Server/Main.cs b/Projects/Server/Main.cs index d25827f6a..d5eae2b32 100644 --- a/Projects/Server/Main.cs +++ b/Projects/Server/Main.cs @@ -346,8 +346,20 @@ public static class Core } } + private static readonly bool UseFastTimestampMath = Stopwatch.Frequency % 1000 == 0; + private static readonly ulong FrequencyInMilliseconds = (ulong)Stopwatch.Frequency / 1000; + [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static long GetTimestamp() => 1000L * Stopwatch.GetTimestamp() / Stopwatch.Frequency; + public static long GetTimestamp() + { + if (UseFastTimestampMath) + { + return (long)((ulong)Stopwatch.GetTimestamp() / FrequencyInMilliseconds); + } + + // Fast calculation will be lossy, fallback to slower but accurate calculation + return (long)((UInt128)Stopwatch.GetTimestamp() * 1000 / (ulong)Stopwatch.Frequency); + } public static void Setup(Assembly applicationAssembly, Process process) {