Java 使用getCurrentThreadUserTime()获取执行时间
我试图测量循环的执行时间,这是一个简单的加法矩阵。 这是我的密码:Java 使用getCurrentThreadUserTime()获取执行时间,java,execution-time,Java,Execution Time,我试图测量循环的执行时间,这是一个简单的加法矩阵。 这是我的密码: //get integers m and n from user before this. long start,end,time; int[][] a = new int[m][n]; int[][] b = new int[m][n]; int[][] c= new int[m][n]; start = getUserTime(
//get integers m and n from user before this.
long start,end,time;
int[][] a = new int[m][n];
int[][] b = new int[m][n];
int[][] c= new int[m][n];
start = getUserTime();
for(int i = 0;i < m;i++)
{
for(int j = 0;j < n;j++)
{
c[i][j] = a[i][j]+b[i][j];
}
}
end = getUserTime();
time = end - start;
/** Get user time in nanoseconds. */
public long getUserTime() {
ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
return bean.isCurrentThreadCpuTimeSupported( ) ?
bean.getCurrentThreadUserTime() : 0L;
}
//在此之前从用户处获取整数m和n。
漫长的开始、结束、时间;
int[]a=新的int[m][n];
int[]b=新的int[m][n];
int[][]c=新的int[m][n];
start=getUserTime();
for(int i=0;i
问题是,有时它返回0,例如当我输入1000作为m和n时。这意味着要添加两个1000x1000矩阵。有时返回0,有时返回15ms(两者都不断重复)
我不知道该相信15毫秒还是0秒。它们之间有很大的区别。
我知道精度取决于操作系统,并不是真正的纳秒精度,但15毫秒远不是精度问题
编辑:这段代码的目的就是测量循环中的CPU性能。因此,如果可能的话,我希望编译器优化和操作系统上下文切换等的影响最小
非常感谢。您应该使用
System.nanoTime()
。()
从文件中:
此方法只能用于测量经过的时间,不适用于
与系统或挂钟时间的任何其他概念相关。价值
返回表示自某个固定但任意的原点以来的纳秒数
时间(可能在将来,因此值可能为负值)。相同的
origin被一个实例中该方法的所有调用使用
Java虚拟机;其他虚拟机实例可能会
使用不同的来源
因此,可以使用nanoTime()
来测量执行时间,因为测量值总是相同的,并且会使用纳秒
将开始时间设置为当前时间
start = System.nanoTime();
在循环结束时,将结束时间设置为当前纳米时间
end = System.nanoTime();
要找出差异,也就是执行所需的时间,只需像你一样减去
为了方便起见,只需更改getUserTime()
即可返回System.nano()
例如:
//get integers m and n from user before this.
long start,end,time;
int[][] a = new int[m][n];
int[][] b = new int[m][n];
int[][] c= new int[m][n];
start = getUserTime();
for(int i = 0;i < m;i++)
{
for(int j = 0;j < n;j++)
{
c[i][j] = a[i][j]+b[i][j];
}
}
end = getUserTime();
// You could use Math.abs() here to handle the situation where
// the values could be negative
time = end - start;
/** Get user time in nanoseconds. */
public long getUserTime() {
return System.nanoTime()
}
//在此之前从用户处获取整数m和n。
漫长的开始、结束、时间;
int[]a=新的int[m][n];
int[]b=新的int[m][n];
int[][]c=新的int[m][n];
start=getUserTime();
for(int i=0;i
您应该使用System.nanoTime()
。()
从文件中:
此方法只能用于测量经过的时间,不适用于
与系统或挂钟时间的任何其他概念相关。价值
返回表示自某个固定但任意的原点以来的纳秒数
时间(可能在将来,因此值可能为负值)。相同的
origin被一个实例中该方法的所有调用使用
Java虚拟机;其他虚拟机实例可能会
使用不同的来源
因此,可以使用nanoTime()
来测量执行时间,因为测量值总是相同的,并且会使用纳秒
将开始时间设置为当前时间
start = System.nanoTime();
在循环结束时,将结束时间设置为当前纳米时间
end = System.nanoTime();
要找出差异,也就是执行所需的时间,只需像你一样减去
为了方便起见,只需更改getUserTime()
即可返回System.nano()
例如:
//get integers m and n from user before this.
long start,end,time;
int[][] a = new int[m][n];
int[][] b = new int[m][n];
int[][] c= new int[m][n];
start = getUserTime();
for(int i = 0;i < m;i++)
{
for(int j = 0;j < n;j++)
{
c[i][j] = a[i][j]+b[i][j];
}
}
end = getUserTime();
// You could use Math.abs() here to handle the situation where
// the values could be negative
time = end - start;
/** Get user time in nanoseconds. */
public long getUserTime() {
return System.nanoTime()
}
//在此之前从用户处获取整数m和n。
漫长的开始、结束、时间;
int[]a=新的int[m][n];
int[]b=新的int[m][n];
int[][]c=新的int[m][n];
start=getUserTime();
for(int i=0;i
您应该使用System.nanoTime()
。()
从文件中:
此方法只能用于测量经过的时间,不适用于
与系统或挂钟时间的任何其他概念相关。价值
返回表示自某个固定但任意的原点以来的纳秒数
时间(可能在将来,因此值可能为负值)。相同的
origin被一个实例中该方法的所有调用使用
Java虚拟机;其他虚拟机实例可能会
使用不同的来源
因此,可以使用nanoTime()
来测量执行时间,因为测量值总是相同的,并且会使用纳秒
将开始时间设置为当前时间
start = System.nanoTime();
在循环结束时,将结束时间设置为当前纳米时间
end = System.nanoTime();
要找出差异,也就是执行所需的时间,只需像你一样减去
为了方便起见,只需更改getUserTime()
即可返回System.nano()
例如:
//get integers m and n from user before this.
long start,end,time;
int[][] a = new int[m][n];
int[][] b = new int[m][n];
int[][] c= new int[m][n];
start = getUserTime();
for(int i = 0;i < m;i++)
{
for(int j = 0;j < n;j++)
{
c[i][j] = a[i][j]+b[i][j];
}
}
end = getUserTime();
// You could use Math.abs() here to handle the situation where
// the values could be negative
time = end - start;
/** Get user time in nanoseconds. */
public long getUserTime() {
return System.nanoTime()
}
//在此之前从用户处获取整数m和n。
漫长的开始、结束、时间;
int[]a=新的int[m][n];
int[]b=新的int[m][n];
int[][]c=新的int[m][n];
start=getUserTime();
for(int i=0;i