RTRT。。
授之以鱼不如授渔。。也教教我怎么查吧。。

解决方案 »

  1.   

    用System.currentTimeMillis();在程序开始的地方得到一次当前时间
    在程序结束的地方得到一次时间,两个时间相减,就是程序运行的毫秒数。
      

  2.   

    怎么测试代码的运行时间。。总的思想就是程序开始的时候开始计时,然后程序结束的时候获取时间,程序开始是从main函数开始的,因此在main函数的开始处开始计时。。看个小代码吧public class Test2 {
    public static void main(String[] args){
    String str="";
    long startTime=System.currentTimeMillis();//开始计时
    //这里得到的时间是一个long型的毫秒数,具体是怎么样一个毫秒数去查API中的System的这个方法
    for(int i=0;i<1000;i++){
    str=str+1;
    }
    long endTime=System.currentTimeMillis();//结束获取结束时的时间
    long runTime=endTime-startTime;//两个时间只差就是运行时间
    System.out.print("程序运行时间是:"+runTime+"毫秒");
    }
    }
      

  3.   

    封装了一个类,以后就可以用了:测试代码:public class Test {    public static void main(String[] args) {
            Clock clock = Clock.getNanoSecondClock();
            clock.start();
            for(int i = 0; i < 10000; i++);
            clock.stop();
            System.out.println(clock.spendTime());
        }
    }/**
     * 用于测量代码执行的计时器
     */
    public abstract class Clock {    private long start;
        private long end;    private Clock() { }    private final static Clock MILLIS_SECOND_CLOCK = new Clock() {
                protected long getCurrentTime() {
                    return System.currentTimeMillis();
                }
            };    private final static Clock NANO_SECOND_CLOCK = new Clock() {
                protected long getCurrentTime() {
                    return System.nanoTime();
                }
            };    /**
         * 获得毫秒级别的计时器
         * @return
         */
        public static Clock getMillisSecondClock() {
            return MILLIS_SECOND_CLOCK;
        }    /**
         * 获得纳秒级别的计时器
         * @return
         */
        public static Clock getNanoSecondClock() {
            return NANO_SECOND_CLOCK;
        }    /**
         * 获得耗时
         * @return
         */
        public long spendTime() {
            if(end == 0L) {
                stop();
            }
            long spend = end - start;
            start = end = 0L;
            return spend;
        }    /**
         * 启动计时器
         */
        public void start() {
            start = getCurrentTime();
        }    /**
         * 停止计时器
         */
        public void stop() {
            end = getCurrentTime();
        }    /**
         * 获得当前计时器的时间
         * @return
         */
        protected abstract long getCurrentTime();
    }
      

  4.   

    把其中的方法改了一下,感觉这样会更好一些:    /**
         * 获得耗时
         * @return
         */
        public long spendTime() {
            if(end == 0L) {
                stop();
            }
            return (end - start);
        }    /**
         * 启动计时器
         */
        public void start() {
            end = 0L;
            start = getCurrentTime();
        }