【发布时间】:2013-08-04 01:00:10
【问题描述】:
使用java尝试解决一个数学问题,并寻找提高解决效率的方法,我得到了非常显着的执行时间大幅增加,不知道它是怎么来的。经过几次测试,我可能已经找到了答案,但我仍然不知道这是如何发生的或为什么会发生。
这里是显示这个时间差的测试代码:
public class arrayAcessTime {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
AccessTime();
}
public static long getLargestPrimeFactor(long l, int[] x)
{
long p = 0, n = l/2, r = (long) Math.sqrt(l), y = 49;
for(long i = 13; i <= r;)
{
for(int j = 0; j<x.length; j++)
{
if (l % i == 0)
{
n = l/i;
}
else
{
n = l / (i + 1);
}
i+=x[j];
}
}
return p;
}
public static long getLargestPrimeFactor(long l)
{
long p = 0, n = l/2, r = (long) Math.sqrt(l), y = 49;
int x[] = {2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 4, 2,
System.out.println("x size: " + x.length);
for(long i = 13; i <= r;)
{
for(int j = 0; j<x.length; j++)
{
if (l % i == 0)
{
n = l/i;
}
else
{
n = l / (i + 1);
}
i+=x[j];
}
}
return p;
}
public static void AccessTime() {
int array2[] = {2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2,....} //too large to write here
long start;
double diff;
System.out.println("Array2 size: " + array2.length);
start = System.currentTimeMillis();
getLargestPrimeFactor(8798765600851475143L, array2);
diff = (System.currentTimeMillis() - start) / 1000.0;
System.out.println("Time: " + diff);
start = System.currentTimeMillis();
getLargestPrimeFactor(8798765600851475143L);
diff = (System.currentTimeMillis() - start) / 1000.0;
System.out.println("Time: " + diff);
}
}
输出:
Array2 size: 5760
Time: 6.144
x size: 5760
Time: 30.225
如您所见,时间增加非常显着(大约 5 倍)。这两种方法几乎相同,只是一种在其中初始化了一个数组,而另一种将初始化的数组作为输入。这如何或为什么会导致时间显着增加?另一方面,对于相当小的数组(
【问题讨论】:
-
@Mysticial:该方法只被调用一次。
-
减少的时间是因为你没有在计时中包括初始化数组需要多长时间。数组在计时的代码部分之外初始化。
-
@KevinCrowell:虽然这是正确的,但我不希望初始化数组需要 24 秒...
-
@JonSkeet 即使是非常大的阵列?他没有提供完整的数组,所以我不确定使用了多少元素。
-
在初始化数组之前尝试开始计数的结果时间,以确保确定(也就是说,在声明数组之前开始计数并在方法之后完成,就像你知道的那样)。
标签: java arrays performance time