perf[time]: volatile reduces the cache hit ratio of the CPU

This commit is contained in:
godotg
2023-02-24 23:11:54 +08:00
parent f4f700b132
commit 11e7cb8bca
2 changed files with 4 additions and 5 deletions
@@ -68,8 +68,6 @@ public abstract class SchedulerBus {
logger.error("scheduler triggers an error.", e);
}
}, 0, TimeUtils.MILLIS_PER_SECOND, TimeUnit.MILLISECONDS);
executor.scheduleAtFixedRate(TimeUtils::currentTimeMillis, 0, 20, TimeUnit.MILLISECONDS);
}
public static class SchedulerThreadFactory implements ThreadFactory {
@@ -83,7 +83,8 @@ public abstract class TimeUtils {
SchedulerBus.refreshMinTriggerTimestamp();
}
private static volatile long timestamp = System.currentTimeMillis();
// volatile reduces the cache hit ratio of the CPU
private static long timestamp = System.currentTimeMillis();
/**
* 获取精确的时间戳
@@ -94,9 +95,9 @@ public abstract class TimeUtils {
}
/**
* 获取最多只有20ms延迟的粗略时间戳,适用于对时间精度要求不高的场景,最多只有20ms误差
* CN获取最多只有一秒延迟的粗略时间戳,适用于对时间精度要求不高的场景,比System.currentTimeMillis()的性能高10倍
* <p>
* 比System.currentTimeMillis()的性能高10倍
* ENObtain a coarse timestamp with a delay of up to one second, which is suitable for scenarios that do not require high time accuracy
*/
public static long now() {
return timestamp;