题目是写一个计算一次循环运行时间的程序
我是这么写的public static void main(String[] args)
{
Date d = new Date();
int sec = d.getSeconds();
int num = 0;
while(d.getSeconds()!=(sec+1)) //计算1秒能运行多少次
{
num = num + 1;
}
System.out.print(1/num);
}
为什么运行不出结果?
我是这么写的public static void main(String[] args)
{
Date d = new Date();
int sec = d.getSeconds();
int num = 0;
while(d.getSeconds()!=(sec+1)) //计算1秒能运行多少次
{
num = num + 1;
}
System.out.print(1/num);
}
为什么运行不出结果?
d已经是一个已知的值,不会动态的变化
sorry,是永远成立,死循环
单纯测试,可以循环两次,循环中用System.currentTimeMillis()得到两次的时间保存到一个long[2]数组,然后相减
public static void main(String[] args) {
System.out.println(new Timestamp(System.currentTimeMillis()));
int num = 0;
for (int i = 0; i < 1; i++) {
num += 1;
}
System.out.println(new Timestamp(System.currentTimeMillis()));
}
}
long start = System.nanoTime();
for(int i = 0 ; i < 100; i++){
num+=1;
}
long ends= System.nanoTime();
System.out.println((ends-start)/100);
这样就可以了,用纳秒计算
public static void main(String[] args)
{
Date d = new Date();
int sec = d.getSeconds();
int num = 0;
while(new Date().getSeconds()!=(sec+1)) //计算1秒能运行多少次
{
num = num + 1;
}
System.out.print(1/num);
}
{
Date d = new Date();
int sec = d.getSeconds();
int num = 0;
while(d.getSeconds()!=(sec+1)) //计算1秒能运行多少次
{
num = num + 1;
d = new Date();
}
System.out.print(1/num);
}
大部分程序80%的时间会执行20%的代码,sun的hotspot虚拟机,它在执行过程中会找出这20%的代码并且编译成本地代码,所以开始执行的时候会很慢,持续执行会变快。
有的jvm实现在一开始时就把所有的字节码编译成本地代码,这样第一次执行的时候肯定最慢,持续执行会很快
还有的jvm实现根本就是解释执行,拿这种jvm执行,一直都很慢所以你用c语言的办法来测试java语言,根本是行不通的!
public class Test {
public static void main(String[] args) {
long step = 1000000000;
long end;
long star = System.nanoTime();
while(step-- > 0);
end = System.nanoTime();
System.out.printf("每次循环使用 %d nm",(end - star) / 1000000000);
}}
System.out.println("循环前运行的时间毫秒:"+beging);
for(int i=0; i<a.size(); i++){
}
System.out.println("循环后运行的时间毫秒:"+(System.currentTimeMillis() - beging));
/*
*计算循环一次所需要的时间
*/
public class JavaTest18
{
public static void main(String [] args)
{
int i = 0;
long begin = System.currentTimeMillis();
while(i < 1000000)
{
Object o = new Object();
i++;
}
long end = System.currentTimeMillis();
System.out.println("it got " + ((end-begin)/1000000.0) + "ms");
}
}创建1000000个object对象只用了1.5*10E-5ms
// TODO Auto-generated method stub long cur1=System.nanoTime();
//int count=1000;
for(int i=0;i<1000;i++){
i++;
}
long cur2=System.nanoTime();
double v=(cur2-cur1)/1000;
System.out.println(v);
}}
TestCurr tc1=new TestCurr(100);
TestCurr tc2=new TestCurr(1000);
TestNano tn1=new TestNano(100);
TestNano tn2=new TestNano(100000);
}}
class TestCurr{
TestCurr(int count){
long cur1=System.currentTimeMillis();
for(int i=0;i<count;i++){
i++;
}
// Thread t=new Thread();
// try {
// t.sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
long cur2=System.currentTimeMillis();
//long v=(cur2-cur1)/count;
System.out.println("使用System.currentTimeMillis() :"+(double)((cur2-cur1)/count));
}
}
class TestNano{
TestNano(int count){
long cur1=System.nanoTime();
for(int i=0;i<count;i++){
i++;
}
long cur2=System.nanoTime();
//long v=(cur2-cur1)/count;
System.out.println("使用System.nanoTime() :"+(double)((cur2-cur1)/count));
}
}
TestCurr tc1=new TestCurr(100);
TestCurr tc2=new TestCurr(1000);
TestNano tn1=new TestNano(100);
TestNano tn2=new TestNano(100000);
}}
class TestCurr{
TestCurr(int count){
long cur1=System.currentTimeMillis();
for(int i=0;i<count;i++){
i++;
}
// Thread t=new Thread();
// try {
// t.sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
long cur2=System.currentTimeMillis();
//long v=(cur2-cur1)/count;
System.out.println("使用System.currentTimeMillis() :"+(double)((cur2-cur1)/count));
}
}
class TestNano{
TestNano(int count){
long cur1=System.nanoTime();
for(int i=0;i<count;i++){
i++;
}
long cur2=System.nanoTime();
//long v=(cur2-cur1)/count;
System.out.println("使用System.nanoTime() :"+(double)((cur2-cur1)/count));
}
}
TestCurr tc1=new TestCurr(100);
TestCurr tc2=new TestCurr(1000);
TestNano tn1=new TestNano(100);
TestNano tn2=new TestNano(100000);
}}
class TestCurr{
TestCurr(int count){
long cur1=System.currentTimeMillis();
for(int i=0;i<count;i++){
i++;
}
// Thread t=new Thread();
// try {
// t.sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
long cur2=System.currentTimeMillis();
//long v=(cur2-cur1)/count;
System.out.println("使用System.currentTimeMillis() :"+(double)((cur2-cur1)/count));
}
}
class TestNano{
TestNano(int count){
long cur1=System.nanoTime();
for(int i=0;i<count;i++){
i++;
}
long cur2=System.nanoTime();
//long v=(cur2-cur1)/count;
System.out.println("使用System.nanoTime() :"+(double)((cur2-cur1)/count));
}
}
public class RunningTime
{
public static void main(String argc[]) throws InterruptedException
{
int i=0,j=0;
long k=0,begin,end,time;
Date mydate=new Date();
begin=mydate.getTime();
for (;i<100;i++)
{
for(;j<100;j++)
{
Thread.currentThread().sleep(5);
k+=i*j;
}
}
Date mydate2=new Date();
end=mydate2.getTime();
time=end-begin;
System.out.println(begin+"运行时间为:"+time+" "+end);
}
}
这是一个java计算程序执行时间的类,可以试试这个,计算一次循环所用的时间。
package com.test;
public class circulateTime { /**
* @param args
* 写一个计算一次循环运行时间的程序
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Long beginTime = System.currentTimeMillis();
for(int i=0;i<999999999;i++)
{
}
Long time = System.currentTimeMillis() - beginTime;
System.out.println("运行时间为:" + time + "毫秒");
}}
想得到一次循环时间,这样应该可以吧:
public static void main(String[] args)
{
long startTime = System.currentTimeMillis();
int num = 0;
while(num<=100000000) //计算1秒能运行多少次
{
num++;
}
long endTime = System.currentTimeMillis();
System.out.println(startTime);
System.out.println(endTime);
System.out.print((double)(endTime-startTime)/num);
}