题目是写一个计算一次循环运行时间的程序
我是这么写的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);
}
为什么运行不出结果?

解决方案 »

  1.   

    while(d.getSeconds()!=(sec+1)) 永远不成立
    d已经是一个已知的值,不会动态的变化
      

  2.   


    sorry,是永远成立,死循环
      

  3.   

    计算程序运行时间请用System.currentTimeMillis()
      

  4.   

    没怎么明白其中的意义,其实循环根据循环体的复杂度不同,时间也会不同的
    单纯测试,可以循环两次,循环中用System.currentTimeMillis()得到两次的时间保存到一个long[2]数组,然后相减
      

  5.   

    public class TimeTest {
    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()));
    }
    }
      

  6.   

    我的思路是让循环运行1秒钟,然后得到1s执行了多少次循环,最后将循环次数取倒数,就是一次的时间,直接用System.currentTimeMillis()求一次的时间求不出结果
      

  7.   

    int num = 0;
    long start = System.nanoTime();
    for(int i = 0 ; i < 100; i++){
    num+=1;
    }
    long ends= System.nanoTime();
    System.out.println((ends-start)/100);
    这样就可以了,用纳秒计算
      

  8.   


    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);
        }
      

  9.   

    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;
                d = new Date();
            }
            
            System.out.print(1/num);
        }
      

  10.   

    JAVA的 DATE和 C#的DATE是不一样的,从这点就可以看出C#高级多了
      

  11.   

    System.currentTimeMillis()取一次的时间没有结果,表明什么?!程序运行时间极短!换句话说,你在程序中不停的取时间的代码的执行时间可能本来就能执行好几次循环了,这样怎么测得准?另外,不同的jvm,这个结果也是有差异的
    大部分程序80%的时间会执行20%的代码,sun的hotspot虚拟机,它在执行过程中会找出这20%的代码并且编译成本地代码,所以开始执行的时候会很慢,持续执行会变快。
    有的jvm实现在一开始时就把所有的字节码编译成本地代码,这样第一次执行的时候肯定最慢,持续执行会很快
    还有的jvm实现根本就是解释执行,拿这种jvm执行,一直都很慢所以你用c语言的办法来测试java语言,根本是行不通的!
      

  12.   


    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);
    }}
      

  13.   

    题目要求一次循环的运行时间,我觉得可以先计算用System.currentTimeMillis()所用的时间t1,再让循环运行10000次,获取循环运行时间t2,用循环运行时间t2/10000就可以得到循环运行一次的时间。当然循环运行次数越多,精度越高。
      

  14.   

    long beging = System.currentTimeMillis();
    System.out.println("循环前运行的时间毫秒:"+beging);
    for(int i=0; i<a.size(); i++){

    }

    System.out.println("循环后运行的时间毫秒:"+(System.currentTimeMillis() - beging));
      

  15.   


    /*
    *计算循环一次所需要的时间
    */
    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
      

  16.   

    public class Test2 { public static void main(String[] args) {
    // 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);
    }}
      

  17.   

    这个可以更好说明问题,import java.util.*;public class Test2 { public static void main(String[] args) {

    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));
    }
    }
      

  18.   

    import java.util.*;public class Test2 { public static void main(String[] args) {

    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));
    }
    }
      

  19.   

    import java.util.*;public class Test2 { public static void main(String[] args) {

    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));
    }
    }
      

  20.   

    import java.util.*;
    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计算程序执行时间的类,可以试试这个,计算一次循环所用的时间。
      

  21.   


    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 + "毫秒");
    }}
      

  22.   

    Date d = new Date();只是获得这句话执行时的当前日期,并且以后没有再次获取所以每次d.getSeconds()方法得到的数值都是一样的,所以while循环里的条件永远都是true,会一直执行的。
    想得到一次循环时间,这样应该可以吧:
    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);   
     }