9

So I've been doing Project Euler, and I found two ways to solve Problem 57. However, there seems to be a huge difference in performance between using a 32bit JDK and a 64bit 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;
}

If I run the fractionSolution on 64 bit JDK, it will take 30 seconds, but on 32bit, it will take 90 seconds.

If I run patternSolution on 64 bit JDK, it will take about 80 ms, but on 32 bit, it will take about 40 ms.

Why is there such a huge difference between the JDKs? Which one should I use?

I'm using Java SE 7, JDK 1.7

Here are screen caps of my program so you don't have to run it. https://i.stack.imgur.com/BeS8F.png

You can tell which JDK it is from the file paths.

user2372445
  • 101
  • 5
  • Please state the JDK version numbers. – Sebastian May 24 '13 at 13:11
  • have you made sure of other possible variables involved? eg running one in client mode and another in server mode, etc – DPM May 24 '13 at 13:16
  • Yep. I'm using Intellj, and I just switch between jdks in the Module Settings. – user2372445 May 24 '13 at 13:19
  • 4
    @user2372445 That's actually a no. The 32-bit JVM uses a different default garbage collector than the 64-bit JVM, so you need to explicitly pass `-server` to the 32-bit JVM to make sure they are using the same configurations. – Sam Harwell May 24 '13 at 13:32
  • Tens of milliseconds is much too low to allow for warm-up, JIT-compilation, timing jitter … – greybeard Dec 27 '15 at 11:17

0 Answers0