程序前
long start= System.currentTimeMillis();
程序后
long end= System.currentTimeMillis();
所需时间
System.out.println("时间为"+(end-start));
举例
package com.my.new1;
public class zs {
public static void main(String[] args) {
long start= System.currentTimeMillis();
for (int i=2;i<=10000;i++)
{boolean is=true;
for (int j=2;j<=Math.sqrt(i);j++)
{
if (i%j==0){
is=false;
break;
}
}
if (is==true)
{
System.out.println(i);
}
is=false;
}
long end= System.currentTimeMillis();
System.out.println("时间为"+(end-start));
}