备案网站名称怎么改,免费微信网站模板下载工具,石家庄建设工程信息网官网,虚拟主机建设二个网站在Java世界中#xff0c;对System.nanoTime#xff08;#xff09;的理解非常好。 总有一些人说它是快速#xff0c;可靠的#xff0c;并且在可能的情况下#xff0c;应该使用它代替System.currentTimemillis#xff08;#xff09;进行计时。 总的来说#xff0c;他绝… 在Java世界中对System.nanoTime的理解非常好。 总有一些人说它是快速可靠的并且在可能的情况下应该使用它代替System.currentTimemillis进行计时。 总的来说他绝对是在撒谎一点也不差但是开发人员应该意识到一些缺点。 同样尽管它们有很多共同点但是这些缺点通常是特定于平台的。 视窗 使用QueryPerformanceCounter API实现功能众所周知该API存在一些问题。 它可能会飞速发展 有人报告说在多处理器计算机上的速度可能非常慢 等等。我花了一些时间上网尝试查找QueryPerformanceCounter的工作原理和作用。 关于该主题尚无明确结论但是有一些帖子可以简要介绍其工作原理。 我会说最有用的大概是说和那人。 当然只要稍作搜索就能找到更多但信息大致相同。 因此如果可用则实现似乎正在使用HPET 。 如果不是则它将TSC与CPU之间的值进行某种形式的同步。 有趣的是 QueryPerformanceCounter承诺返回的值将以恒定的频率增加。 这意味着在使用TSC和多个CPU的情况下可能不仅会遇到一些困难不仅因为CPU可能具有不同的TSC值而且可能具有不同的频率。 请牢记所有注意事项Microsoft 建议使用SetThreadAffinityMask来阻塞将QueryPerformanceCounter调用到单个处理器的线程这显然不会在JVM中发生。 LINUX Linux与Windows非常相似除了它更加透明我设法下载了源代码:)。 该值从带有CLOCK_MONOTONIC标志的clock_gettime中读取对于真正的男人源可从Linux源的vclock_gettime.c中获得。 使用TSC或HPET 。 与Windows的唯一区别是Linux甚至不尝试同步从不同CPU读取的TSC值而是按原样返回它。 这意味着该值可以在读取时依赖于CPU的依赖关系上来回跳。 另外与Windows签约时Linux不会保持更改频率恒定。 另一方面它绝对应该提高性能。 索拉里斯 Solaris很简单。 我相信通过gethrtime可以实现与linux差不多相同的clock_gettime实现。 区别在于Solaris保证计数器不会跳回这在Linux上是可能的但是有可能返回相同的值。 从源代码可以看出这种保证是使用CAS实现的它需要与主存储器同步并且在多处理器机器上可能相对昂贵。 与Linux相同更改率可能有所不同。 结论 结论是多云之王。 开发人员必须意识到功能不是完美的它可以向后或向前跳跃。 它可能不会单调变化并且变化率会随CPU时钟速度的变化而变化。 而且它并没有许多人想象的那么快。 在我的Windows 7计算机上进行单线程测试时它仅比System.currentTimeMillis快约10而在多线程测试中线程数与CPU数相同只是相同。 因此总的来说它所提供的只是分辨率的提高这在某些情况下可能很重要。 最后要注意的是即使CPU频率没有变化也不要认为您可以将该值可靠地映射到系统时钟请参见此处的详细信息。 附录 附录包含针对不同操作系统的功能实现。 源代码来自OpenJDK v.7。 的Solaris // gethrtime can move backwards if read from one cpu and then a different cpu
// getTimeNanos is guaranteed to not move backward on Solaris
inline hrtime_t getTimeNanos() {if (VM_Version::supports_cx8()) {const hrtime_t now gethrtime();// Use atomic long load since 32-bit x86 uses 2 registers to keep long.const hrtime_t prev Atomic::load((volatile jlong*)max_hrtime);if (now prev) return prev; // same or retrograde time;const hrtime_t obsv Atomic::cmpxchg(now, (volatile jlong*)max_hrtime, prev);assert(obsv prev, invariant); // Monotonicity// If the CAS succeeded then were done and return now.// If the CAS failed and the observed value obs is now then// we should return obs. If the CAS failed and now obs prv then// some other thread raced this thread and installed a new value, in which case// we could either (a) retry the entire operation, (b) retry trying to install now// or (c) just return obs. We use (c). No loop is required although in some cases// we might discard a higher now value in deference to a slightly lower but freshly// installed obs value. Thats entirely benign -- it admits no new orderings compared// to (a) or (b) -- and greatly reduces coherence traffic.// We might also condition (c) on the magnitude of the delta between obs and now.// Avoiding excessive CAS operations to hot RW locations is critical.// See http://blogs.sun.com/dave/entry/cas_and_cache_trivia_invalidatereturn (prev obsv) ? now : obsv ;} else {return oldgetTimeNanos();}
} 的Linux jlong os::javaTimeNanos() {if (Linux::supports_monotonic_clock()) {struct timespec tp;int status Linux::clock_gettime(CLOCK_MONOTONIC, tp);assert(status 0, gettime error);jlong result jlong(tp.tv_sec) * (1000 * 1000 * 1000) jlong(tp.tv_nsec);return result;} else {timeval time;int status gettimeofday(time, NULL);assert(status ! -1, linux error);jlong usecs jlong(time.tv_sec) * (1000 * 1000) jlong(time.tv_usec);return 1000 * usecs;}
} 视窗 jlong os::javaTimeNanos() {if (!has_performance_count) {return javaTimeMillis() * NANOS_PER_MILLISEC; // the best we can do.} else {LARGE_INTEGER current_count;QueryPerformanceCounter(¤t_count);double current as_long(current_count);double freq performance_frequency;jlong time (jlong)((current/freq) * NANOS_PER_SEC);return time;}
} 参考 System.nanoTime背后是什么 来自我们的JCG合作伙伴 Stas博客上的 Stanislav Kobylansky。 热点虚拟机内部时钟计时器和计划事件 当心QueryPerformanceCounter 为Windows实施持续更新的高分辨率时间提供程序 游戏计时和多核处理器 高精度事件计时器维基百科 时间戳计数器维基百科 翻译自: https://www.javacodegeeks.com/2012/02/what-is-behind-systemnanotime.html