9

所以我一直在做Project Euler,我找到了两种解决问题57的方法。但是,使用32位JDK和64位JDK在性能上似乎存在巨大差异。

public static int fractionSolution (int l){
int count = 0;
for (int i = 1; i < l; i++) {
    BigFraction f = iterateFraction(i).subtract(new BigFraction(1,1));
    if(f.getDenominator().toString().length() < f.getNumerator().toString().length()){
        count++;
    }
}
return count;
}

public static BigFraction iterateFraction(int n){
    if(n==1){
        return new BigFraction(2.5);
    }
    else{
        BigFraction base = new BigFraction(1,1);
        BigFraction two = new BigFraction(2,1);
        return two.add(base.divide(iterateFraction(n-1)));
    }
}

public static int patternSolution (int l){
    BigInteger n = new BigInteger("3");
    BigInteger d = new BigInteger("2");
    int count = 0;
    for (int i = 1; i < l; i++) {
        n = n.add(d.multiply(new BigInteger("2")));
        d = n.subtract(d);
        if(n.toString().length() > d.toString().length()){
            count++;
        }
    }
    return count;
}

如果我在 64 位 JDK 上运行 fractionSolution,需要 30 秒,但在 32 位上,需要 90 秒。

如果我在 64 位 JDK 上运行 patternSolution,大约需要 80 毫秒,但在 32 位上,大约需要 40 毫秒。

为什么JDK之间存在如此巨大的差异?我应该使用哪一个?

我正在使用 Java SE 7、JDK 1.7

这是我的程序的屏幕截图,因此您不必运行它。 http://imgur.com/elzQMi3,BxEZ1RK,lDo6YBW,rRiaelE#0

您可以从文件路径中判断它是哪个 JDK。

4

0 回答 0