利用方法计算程序段的运行时间

来源:互联网 发布:北大有没有windows正版 编辑:程序博客网 时间:2024/04/28 10:18
abstract class GetTime {
    public final void getTime() {
        long start = System.currentTimeMillis();
        runcode();
        long end = System.currentTimeMillis();
        System.out.println("运行时间:" + (end - start) + "毫秒");//应该是end - start
    }
 
    public abstract void runcode();
}
 
public class SubTime extends GetTime {//建立一个java文件为SubTime.java,SubTime为主类,加为public
    public void runcode() {
        for (int x = 0; x < 4000; x++) {
            System.out.println(x);
        }
    }
 
    static public void main(String args[]) {//写一个主函数就好了
        new SubTime().getTime();//建立对象调用getTime();实际上是调用了子类的方法以后,并且计算时间显示最后的结果的。
    }
}
1 0
原创粉丝点击