If you run the following example on Linux and compare the
Client VM versus the Server VM, you will notice that the with the client VM,
the testcase does not terminate :
public class Hailstone {
public static void main(String[] args) {
// System.out.println(827370449 * 3 + 1);
long start = System.currentTimeMillis();
int min = 2;
int max = Integer.MAX_VALUE;
System.out.println("maxMove:" + maxMoves(min, max));
System.out.println("time difference: " +
(System.currentTimeMillis() - start));
}
/**
* Imperative implementation that returns the length hailstone moves
for a given number.
*/
public static long hailstoneLengthImp(long n) {
long moves = 0;
while (n != 1) {
assert n > 1;
if (isEven(n)) {
n = n / 2;
} else {
n = 3 * n + 1;
}
++moves;
}
return moves;
}
private static boolean isEven(long n) {
return n % 2 == 0;
}
/**
* Returns the maximum length of the hailstone sequence for numbers
* between min to max.
*
* For rec1 - Assume that min is bigger than max.
*/
public static long maxMoves(int min, int max) {
long maxmoves = 0;
for (int n = min; n <= max; n++) {
System.out.println(n);
long moves = hailstoneLengthImp(n);
if (moves > maxmoves) {
maxmoves = moves;
}
}
return maxmoves;
}
}
% java -client -version
java version "1.6.0_21-ea"
Java(TM) SE Runtime Environment (build 1.6.0_21-ea-b05)
Java HotSpot(TM) Client VM (build 17.0-b15, mixed mode)
Client VM versus the Server VM, you will notice that the with the client VM,
the testcase does not terminate :
public class Hailstone {
public static void main(String[] args) {
// System.out.println(827370449 * 3 + 1);
long start = System.currentTimeMillis();
int min = 2;
int max = Integer.MAX_VALUE;
System.out.println("maxMove:" + maxMoves(min, max));
System.out.println("time difference: " +
(System.currentTimeMillis() - start));
}
/**
* Imperative implementation that returns the length hailstone moves
for a given number.
*/
public static long hailstoneLengthImp(long n) {
long moves = 0;
while (n != 1) {
assert n > 1;
if (isEven(n)) {
n = n / 2;
} else {
n = 3 * n + 1;
}
++moves;
}
return moves;
}
private static boolean isEven(long n) {
return n % 2 == 0;
}
/**
* Returns the maximum length of the hailstone sequence for numbers
* between min to max.
*
* For rec1 - Assume that min is bigger than max.
*/
public static long maxMoves(int min, int max) {
long maxmoves = 0;
for (int n = min; n <= max; n++) {
System.out.println(n);
long moves = hailstoneLengthImp(n);
if (moves > maxmoves) {
maxmoves = moves;
}
}
return maxmoves;
}
}
% java -client -version
java version "1.6.0_21-ea"
Java(TM) SE Runtime Environment (build 1.6.0_21-ea-b05)
Java HotSpot(TM) Client VM (build 17.0-b15, mixed mode)
- duplicates
-
JDK-5091921 Sign flip issues in loop optimizer
-
- Closed
-