JAVA 获取一段程序运行时间
求教,要打印一段程序运行时间,这段代码接下来要怎么写啊?abstractclassGetTime{publicfinalvoidgetTime(){longstart=S...
求教,要打印一段程序运行时间,这段代码接下来要怎么写啊?
abstract class GetTime
{
public final void getTime()
{
long start = System.currentTimeMillis();
runcode();
long end = System.currentTimeMillis();
System.out.println("运行时间:"+(start-end)+"毫秒");
}
public abstract void runcode();
}
class SubTime extends GetTime
{
public void runcode()
{
for(int x = 0; x<4000; x++)
{
System.out.print(x);
}
}
} 展开
abstract class GetTime
{
public final void getTime()
{
long start = System.currentTimeMillis();
runcode();
long end = System.currentTimeMillis();
System.out.println("运行时间:"+(start-end)+"毫秒");
}
public abstract void runcode();
}
class SubTime extends GetTime
{
public void runcode()
{
for(int x = 0; x<4000; x++)
{
System.out.print(x);
}
}
} 展开
2个回答
展开全部
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();
}
}
可以用eclipse运行
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询