/**
* 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.
*
* <p> See the description of the class <code>Date</code> for
* a discussion of slight discrepancies that may arise between
* "computer time" and coordinated universal time (UTC).
*
* @return the difference, measured in milliseconds, between
* the current time and midnight, January 1, 1970 UTC.
* @see java.util.Date
*/
public static native long currentTimeMillis();
以上是jdk源码中的方法currentTimeMillis(),不太明太他怎么就能获取当期系统时间的毫秒数。
System是一个类呀,不是接口。是不是native的原因
* 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.
*
* <p> See the description of the class <code>Date</code> for
* a discussion of slight discrepancies that may arise between
* "computer time" and coordinated universal time (UTC).
*
* @return the difference, measured in milliseconds, between
* the current time and midnight, January 1, 1970 UTC.
* @see java.util.Date
*/
public static native long currentTimeMillis();
以上是jdk源码中的方法currentTimeMillis(),不太明太他怎么就能获取当期系统时间的毫秒数。
System是一个类呀,不是接口。是不是native的原因
解决方案 »
- java 如何知道函数返回的数组的元素个数
- 初学者问题:不用equals判断两个数组是否相等?谢谢!
- java 24点 算法
- 如何获取匿名内部类的变量,请高手指点,有代码!!!
- 我想问问!!我做的聊天室程序,我在本机上连接2个客户端试试都可以连通,但是我发给别的主机,为什么他就连接不上我呢?而且提示有java.
- SAX2 driver class org.apache.xerces.parsers.SAXParser not found 问题
- 搜索未遂,前来求助——按钮双击问题
- 求助??为什么不能赋值??
- JTextArea刷新问题----------------------------
- 北京有没有JAVA的培训班?
- 新手问一个new treeset的问题
- 看似简单
native的方法,想想看
http://topic.csdn.net/u/20091215/10/82819869-6242-4886-954c-e55cad5525f3_3.html#r_62356800System.currentTimeMillis();
在 Windows 平台上的 JDK 实现是采用 windows.h 函数 GetSystemTimeAsFileTime 实现的
GetSystemTimeAsFileTime API: http://msdn.microsoft.com/en-us/library/ms724397%28VS.85%29.aspx
据悉这个时间的精度在 10ms 左右。System.nanoTime();
在 Windows 平台上是使用 QueryPerformanceCounter 和 QueryPerformanceFrequency 这两个函数实现的据称 Windows 平台上这两种方式都有一定的缺陷。与 Windows 平台 JVM 原理相同的 C++ 代码如下(很多代码直接取自 Windows JVM 的源代码),程序运行后会在屏幕上输出与 JDK 那两个函数相同的 nano 和 millis 值: #include <windows.h>
#include <iostream>#define NANOS_PER_SEC 1000000000typedef __int32 jint;
typedef __int64 jlong;
typedef unsigned __int32 juint;
typedef unsigned __int64 julong;using namespace std;static jlong _calculated_offset = 0;
static int _has_calculated_offset = 0;static void set_low(jlong* value, jint low) {
*value &= (jlong)0xffffffff << 32;
*value |= (jlong)(julong)(juint)low;
}static void set_high(jlong* value, jint high) {
*value &= (jlong)(julong)(juint)0xffffffff;
*value |= (jlong)high << 32;
}static jlong jlong_from(jint h, jint l) {
jlong result = 0;
set_high(&result, h);
set_low(&result, l);
return result;
}jlong as_long(LARGE_INTEGER x) {
jlong result = 0;
set_high(&result, x.HighPart);
set_low(&result, x.LowPart);
return result;
}jlong offset() {
if (_has_calculated_offset) return _calculated_offset;
SYSTEMTIME java_origin;
java_origin.wYear = 1970;
java_origin.wMonth = 1;
java_origin.wDayOfWeek = 0; // ignored
java_origin.wDay = 1;
java_origin.wHour = 0;
java_origin.wMinute = 0;
java_origin.wSecond = 0;
java_origin.wMilliseconds = 0;
FILETIME jot;
SystemTimeToFileTime(&java_origin, &jot);
_calculated_offset = jlong_from(jot.dwHighDateTime, jot.dwLowDateTime);
_has_calculated_offset = 1;
return _calculated_offset;
}jlong windows_to_java_time(FILETIME wt) {
jlong a = jlong_from(wt.dwHighDateTime, wt.dwLowDateTime);
return (a - offset()) / 10000;
}int main ()
{
LARGE_INTEGER current_count, frequence; QueryPerformanceCounter(¤t_count);
QueryPerformanceFrequency(&frequence);
jlong j_current_count = as_long(current_count);
jlong j_frequence = as_long(frequence);
cout << "nano: " << (jlong)((double)j_current_count / (double)j_frequence * NANOS_PER_SEC) << endl; FILETIME ft;
GetSystemTimeAsFileTime(&ft);
cout << "millis: " << windows_to_java_time(ft) << endl;
getchar();
return 0;
}
nanoTime这个方法相对准确,你可以通过
long start = System.nanoTime;
(System.nanoTime-start)/1000000D ;得到毫秒。
public class JavaTimeWinTest
{
public static void main(String[] args)
{
msTest();
nsTest();
} private static void msTest()
{
int size = 6000000;
long[] times = new long[size]; for(int k = 0 ;k <size;k++)
{
times[k] = System.currentTimeMillis();
} long preTime = times[0] ;
for(int k = 1 ;k <size;k++)
{
if(times[k]>preTime)
{
System.out.print("ms 当前值:" + times[k]);
System.out.println(" 变化值:" + (times[k]-preTime));
preTime = times[k];
}
}
} private static void nsTest()
{
int size = 50;
long[] times = new long[size]; for(int k = 0 ;k <size;k++)
{
times[k] = System.nanoTime();
} long preTime = times[0] ;
for(int k = 1 ;k <size;k++)
{
if(times[k]>preTime)
{
System.out.print("ns 当前值:" + times[k]);
System.out.println(" 变化值:" + (times[k]-preTime));
preTime = times[k];
}
}
}
}
运行代码输出结果如下:
ms 当前值:1262223891000 变化值:16
ms 当前值:1262223891015 变化值:15
ms 当前值:1262223891031 变化值:16
ms 当前值:1262223891046 变化值:15
ms 当前值:1262223891062 变化值:16
ms 当前值:1262223891078 变化值:16
ms 当前值:1262223891093 变化值:15
ms 当前值:1262223891109 变化值:16
ms 当前值:1262223891125 变化值:16
ms 当前值:1262223891140 变化值:15
ms 当前值:1262223891156 变化值:16
ms 当前值:1262223891171 变化值:15
ms 当前值:1262223891187 变化值:16
ms 当前值:1262223891203 变化值:16
ms 当前值:1262223891218 变化值:15
ms 当前值:1262223891234 变化值:16
ms 当前值:1262223891250 变化值:16
ms 当前值:1262223891265 变化值:15
ms 当前值:1262223891281 变化值:16
ms 当前值:1262223891296 变化值:15
ns 当前值:4634097766894 变化值:3631
ns 当前值:4634097768012 变化值:1118
ns 当前值:4634097769129 变化值:1117
ns 当前值:4634097769967 变化值:838
ns 当前值:4634097771085 变化值:1118
ns 当前值:4634097771923 变化值:838
ns 当前值:4634097773040 变化值:1117
ns 当前值:4634097773879 变化值:839
ns 当前值:4634097774996 变化值:1117
ns 当前值:4634097775834 变化值:838
ns 当前值:4634097776952 变化值:1118
ns 当前值:4634097777790 变化值:838
ns 当前值:4634097778907 变化值:1117
ns 当前值:4634097780025 变化值:1118
ns 当前值:4634097780863 变化值:838
ns 当前值:4634097781980 变化值:1117
ns 当前值:4634097782818 变化值:838
ns 当前值:4634097783936 变化值:1118
ns 当前值:4634097784774 变化值:838
ns 当前值:4634097785891 变化值:1117
ns 当前值:4634097786729 变化值:838
ns 当前值:4634097787847 变化值:1118
ns 当前值:4634097788685 变化值:838
ns 当前值:4634097789802 变化值:1117
ns 当前值:4634097790920 变化值:1118
ns 当前值:4634097791758 变化值:838
ns 当前值:4634097792875 变化值:1117
ns 当前值:4634097793714 变化值:839
ns 当前值:4634097794831 变化值:1117
ns 当前值:4634097795669 变化值:838
ns 当前值:4634097796787 变化值:1118
ns 当前值:4634097797625 变化值:838
ns 当前值:4634097798742 变化值:1117
ns 当前值:4634097799580 变化值:838
ns 当前值:4634097800698 变化值:1118
ns 当前值:4634097801815 变化值:1117
ns 当前值:4634097802653 变化值:838
ns 当前值:4634097803771 变化值:1118
ns 当前值:4634097804609 变化值:838
ns 当前值:4634097805726 变化值:1117
ns 当前值:4634097806564 变化值:838
ns 当前值:4634097807682 变化值:1118
ns 当前值:4634097808520 变化值:838
ns 当前值:4634097809637 变化值:1117
ns 当前值:4634097810475 变化值:838
ns 当前值:4634097811593 变化值:1118
ns 当前值:4634097812431 变化值:838
ns 当前值:4634097813548 变化值:1117
ns 当前值:4634097814666 变化值:1118本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/chuan122345/archive/2009/12/31/5110727.aspx
long start = System.currentTimeMillis();
long end = System.currentTimeMillis();
long time = end - start;
这里也就发现一个问题,这里出来的时间都是毫秒,所以低于毫秒的时间不能表示出来。所以就很郁闷。不过,听说,java 1.5版本开始支持更小的时间单位,可以到纳秒,网上搜索了下资料,果然发现了更精确的时间单位:纳秒
于是可以这样计算耗费时间了:
long start = System.nanoTime();
long end = System.nanoTime();
long time = end - start;
测试下果然很精确,了解了下纳秒,原来纳秒的单位很小:
1秒 = 109纳秒 1秒 = 103毫秒
那么,纳秒是最小的时间单位吗,仔细搜索了下,大吃一惊,原来还有更小的时间时间单位:皮秒,飞秒
而且毫秒和纳秒之间还有一个微秒,这些单位之间的换算都是1000,换算如下:
1秒 = 1000000000000000飞秒 = 1000000000000皮秒 = 1000000000纳秒 = 1000000微秒 = 1000毫秒
1秒 = 1000000000000皮秒 = 1000000000纳秒 = 1000000微秒 = 1000毫秒
1秒 = 1000000000纳秒 = 1000000微秒 = 1000毫秒
1秒 = 1000000微秒 = 1000毫秒
1秒 = 1000毫秒下边统一介绍下各时间单位:
毫秒,时间单位,符号ms(英语:millisecond ),一秒等于1000毫秒,等于10的15次方皮秒.
微秒,时间单位,符号μs(英语:microsecond ).
纳秒,时间单位,ns(nanosecond),一秒的10亿分之一,即等于10的负9次方秒。常用作内存读写速度的单位,其前面数字越小表示速度越快
皮秒,时间单位,符号ps(英语:picosecond ).
飞秒,时间单位,fs(femtosecond),是标衡时间长短的一种计量单位。1飞秒只有1秒的一千万亿分之一,即1e?15秒或0.001皮秒(1皮秒是,1e?12秒)。即使是每秒飞行30万千米的光速,在一飞秒内,也只能走0.3微米,不到一根头发丝的百分之一。可见光的振荡周期为1.30到2.57飞秒。总结:原来飞秒才是时间的最小单位,至少目前是。但是计算机系统都支持这些时间吗?我只知道目前JDK 1.5只支持毫秒和纳秒。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/chuan122345/archive/2009/12/11/4985463.aspx