`
hold_on
  • 浏览: 451945 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

System.nanoTime() 的隐患

阅读更多

      前段时间项目中需要 统计接口连接时间,考虑到连接时间一般都是零点几毫秒级别的,为了拿到更精确地数值,没有使用System.currentTimeMillis(),而是贸然地使用System.nanoTime()来统计时间,后来分析服务器上的数据,发现 竟然有10-15%的数据数值竟然超过了 10的13次方。

     原因:

System.currentTimeMillis() 起始时间是基于 1970.1.1 0:00:00 这个确定的时间的,而System.nanoTime()是基于cpu核心的时钟周期来计时,它的开始时间是不确定的。(有篇文章说是更加cpu核心的启动时间开始计算的)

但是在多核处理器上,由于每个核心的开始时间不确定,但是在多核处理器上,

	long start = System.nanoTime();
		String ip = Utilities.getIpByUrl(url);
		long cost = System.nanoTime() - start;

 

 这段代码有可能会运行在两个不同的cpu核心上,从而导致得到的结果完全不符逻辑。

 

Returns the current timestamp of the most precise timer available on the local system, in nanoseconds. Equivalent to Linux's CLOCK_MONOTONIC.

This timestamp should only be used to measure a duration by comparing it against another timestamp from the same process on the same device. Values returned by this method do not have a defined correspondence to wall clock times; the zero value is typically whenever the device last booted. Use currentTimeMillis() if you want to know what time it is. 

     

0
0
分享到:
评论
7 楼 yhxf_ie 2018-07-24  
zhangnanw 写道
亲,这不是纳秒时间的隐患,是作者用错了地方。

统计接口连接时间长度, 怎么用错了地方?
6 楼 zhangnanw 2015-11-27  
亲,这不是纳秒时间的隐患,是作者用错了地方。
5 楼 somatezyc 2015-10-22  
楼主,我是用System.currentTimeMillis()计算差值出现过错误的情况,当时是因为调整时钟导致的。所以使用这个也是不对的。
4 楼 hold_on 2014-03-13  
liang86 写道
System.currentTimeMillis() 起始时间是基于 1970.1.1 0:00:00 这个确定的时间的.

为什么两个时间相减,会不正确呢?


应该是System.nanoTime(),已更正,谢谢提醒
3 楼 liang86 2014-03-12  
System.currentTimeMillis() 起始时间是基于 1970.1.1 0:00:00 这个确定的时间的.

为什么两个时间相减,会不正确呢?
2 楼 TeacherMao 2013-09-18  
又仔细阅读了一下在1楼引用的文章,发现在intel平台上win xp sp2之前以及在linux 2.6.18之前都是有问题的,之后应该都修好了。基本上说,nanoTime更依赖于cpu的设计和操作系统在这个级别上的时间函数的实现,所以谨慎对待还是很有必要的。
1 楼 TeacherMao 2013-09-17  
这篇文章有点儿耸人听闻,希望楼主可以给出确实的可验证的例子。

我特地查了一些资料,看到了这篇文章:http://stackoverflow.com/questions/510462/is-system-nanotime-completely-useless

从这篇文章的答案中看,你说的很可能是个误解。当然如果你有一个在多核下面很容易复制的样例,比什么理论都有说服力。期待楼主的跟进。

相关推荐

    android时间戳总结:System.nanoTime(),System.currentTimeMillis(),SystemClock

    System.nanoTime() System.currentTimeMillis() SystemClock.uptimeMillis() SystemClock.elapsedRealtime() SystemClock.currentThreadTimeMillis 0、时间的单位: 秒:second 毫秒:Millisecond 微妙:Microsecond...

    TESTcode:测试代码。 简单密码,吸血鬼编号,频率分析器和System.nanoTime();

    测试代码 测试代码。 简单密码 吸血鬼号码 频率分析仪和System.nanoTime(); PCG 谨防! 这些只是beta版之前的一部分; )

    momomo.com.platform.Nanotime:从Java Runtime要求时间时允许更高的精度。 即纳秒精度

    momomo.com.platform.Nanotime:从Java Runtime要求时间时允许更高的精度。 即纳秒精度

    JAVA版本标准纳秒实现

    2、解决System.nanoTime();并不代表实际时间纳秒的问题。 3、解决解决System.nanoTime();不同JVM获取值不一致的问题。 4、解决解决System.nanoTime();值无法与毫秒时间换算问题。 5、可与GO的纳秒时间兼容。

    nanotime:返回当前纳米时间。 用于制作 id

    纳米时间返回当前纳米时间。 用于制作 id。执照国际学习中心

    jedis使用指南

    while ((System.nanoTime() - nano) ) { if (jedis.setnx(key, LOCKED) == 1) { jedis.expire(key, EXPIRE); locked = true; return locked; } // 短暂休眠,nano避免出现活锁 Thread.sleep(3, r.nextInt...

    《JAVA游戏程序设计教程》实验任务书.pdf

    //让线程休眠,由 sleepTime 值决定 } catch (InterruptedException ex) { } t1 = System.nanoTime(); //重新获取当前系统时间 } } 3. 在游戏逻辑更新部分实现小球的运动逻辑,使得小球可以朝着窗口四周 移动,并在...

    ultra-simple-microbenchmark

    超简单微基准有关如何对代码进行基准测试的简单演示System.nanoTime等的幼稚方法JMH的最佳方法。

    多维时间序列记录库NetflixSpectator.zip

     requestLatency.record(System.nanoTime() - s, TimeUnit.NANOSECONDS);  }  }  public int getNumConnections() {  // however we determine the current number of connections on the ...

    CS146_DataShuffling

    CS146_DataShuffling A部分–数据混排建议的解决方案: 我通过读取给定文件并... 时间变量设置为System.nanoTime()。 这是执行任何方法之前系统的初始时间。 需要考虑的案例: 如果找不到文件ErdosCA.txt和ShaoAnniS

    jvm-micro-benchmarks:JVM代码的微基准

    jvm-micro基准 JVM代码的微基准。 一些基准测试使用JMH进行延迟测试。 协同遗漏 一些基准是手写的,以补偿协调的遗漏。 协调遗漏会严重降低...long next = System . nanoTime(); int interval = ( int ) ( 1e9 / rat

    排列数问题

    对于一个m位整数,我们... long startTime = System.nanoTime(); int iTest; String fileName = "COUNT0.IN"; iTest = ReadFromFile.readFileByChars(fileName); Arith r = new Arith (); r.Arith(iTest); } }

    timers:提供纳秒计时器的实用程序类

    计时器 一个实用程序类,它基于通过调用System.nanoTime()提供的纳秒级时间戳提供一个简单的计时器。 计时器提供以下操作: 重置 开始 分裂 暂停 恢复 停止 计时器状态为: 统一 跑步 已暂停 已停止

    Benchmark-Algorithms:Java中的Sorting Algorithms基准测试应用程序

    我们通过使用Java的System.nanoTime()函数来做到这一点,即在调用算法之前就启动时钟,并在返回算法后立即停止时钟。 之后,我们将时间转换为毫秒以进行分析,在此我们计算每种输入大小下每种算法在10

    cache-strategy

    缓存策略入门例子1 @CacheStrategy ( cacheName = " cachename2 " )public Map< String> getUserUids( String entityId, @CacheKey List< String> userIds) { ... identity(), userId - > System . nanoTime()));}

    node-nanotime:当前时间(以纳秒为单位)

    纳米时间获取当前时间(以纳秒或微秒为单位)。 在node.js中,您可以使用process.hrtime()获得高分辨率时间,但这是来自未知的相对时间,而不是纪元。 因此,该库通过计算时差并将其添加到当前时间来帮助实现这一点...

    Direct Transistor-Level Layout for Digital Blocks

    d) Verify the timing with Nanotime and spice. The book is good as far as message it is conveying but the problem is that the layout tool has to be "very very" good in handling the layouts. At this ...

    Android OpenGL ES 简明开发教程

    Android OpenGL ES 简明开发教程

    Metodologia_Ejercicios:编程方法和算法应用练习

    方法论_练习 编程方法和算法应用练习 实用程序:我们正在研究的算法的最重要的课程 斐波那契:迭代和递归 ... 并使用 System 类的 currentTimeMillis 和/或 nanoTime 方法,它们提供分别以毫秒和纳秒为单位的系统时间。

    PauseIntrinsics

    此测试将证明在平台上可能存在的最佳情况下线程到线程延迟的最佳估计值(如果测量时间存在延迟)使用Stopwatch.GetTimestamp()可以打折(可以使用...GetTimestamp.Benchmark.Cli测试在百分比范围内分别估计nanoTime延迟)...

Global site tag (gtag.js) - Google Analytics