如何提高 System.currentTimeMillis() 粒度? [英] How to improve System.currentTimeMillis() granularity?

查看:64
本文介绍了如何提高 System.currentTimeMillis() 粒度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不提供非常大的数组作为输入的情况下实现它?我正在测量不同算法的运行时间,对于 20 个元素的数组,我得到了非常(相同)相似的值.我尝试将总时间除以 1000000000 以清除 E,然后使用 16 个镜像,在其中复制输入数组并再次为镜像执行.但是对于堆排序和快速排序来说仍然是一样的.不需要写多余的行的任何想法?

How can I achieve it without giving as input very large arrays? I am measuring the running time of different algorithms and for an array of 20 elements I get very (the same) similar values. I tried divided the total time by 1000000000 to clear of the E and then used like 16 mirrors where I copied the input array and executed it again for the mirror. But still it is the same for Heap and Quick sort. Any ideas without needing to write redundant lines?

示例输出:

Random array:
MergeSort:
Total time 14.333066343496
QuickSort:
Total time 14.3330663435256
HeapSort:
Total time 14.3330663435256 

如果您需要代码片段,请通知我.

If you need code snippets just notify me.

推荐答案

您无法提高此方法的粒度.根据 Java SE 文档:

You can't improve the granularity of this method. According to Java SE documentation:

以毫秒为单位返回当前时间.注意,虽然单位返回值的时间是毫秒,粒度值取决于底层操作系统并且可能更大.例如,许多操作系统以几十个单位来测量时间.毫秒.

Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

(来源)

正如其他人所说,对于时间流逝,public static long nanoTime() 会给你更高的精度,但不是分辨率:

As others stated, for time lapses, public static long nanoTime() would give you more precision, but not resolution:

此方法提供纳秒级精度,但不一定纳秒级分辨率.

This method provides nanosecond precision, but not necessarily nanosecond resolution.

(来源)

这篇关于如何提高 System.currentTimeMillis() 粒度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆