????????
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????д?????????????????????????????????У??????????????????????????????????????????????????????????????????????????????????????????????????????????????Щ????????????????????????????????????????????ж???
??????????????????У?????????????????????????????????????????????????????????????????????????£?????????????о????????????顣????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Щ??????????????????????????????????????????????????????????м????????
???????????е???????????????
????????????????????????????????????????????????????????ò????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
???????????????????????????????????????н??е??????????????????????????????????????????????????????????????????????????????????????????????????????????????????е???????????????????????????????????Щ??????????????????????????????????????????Щ?????????????????????????????????????????????????????????д???????????????д??????????????????????????????????????????????????????з????Щ??????????????????????Σ???????? 50 ????????????????з???????????????????????
public void doTest() {
double l;
long then = System.currentTimeMillis();
int nLoops = 50;
for (int i = 0; i < nLoops; i++) {
l = compute(50);
}
long now = System.currentTimeMillis();
System.out.println("Elapsed time:" + (now - then));
}
private double compute(int n){
if (n < 0)
throw new IllegalArgumentException("Must be > 0");
if (n == 0)
return 0d;
if (n == 1)
return 1d;
double d = compute(n - 2) + compute(n - 1);
if (Double.isInfinite(d))
throw new ArithmeticException("Overflow");
return d;
}
?????????δ?????????????????????????????ж??????????????????????????????????????????????й????? compute ??????????е????????????????????????????????????????? double ?????“l”???? volatile ?????????????????????????????????????????????????????? volatile ???ε?????????????????????????????????????????????
??????????????????????????????д????????????????????????缸???????????С??????????????????????????????????????????????????????????????????????????????????????????????????в?????????????????????????????????????????????????????ò??????
?????????????????????????????????????????????????????????????????????С???????????????????佫??????????????С?????????????????????????????? 50%?????????????????????????????????????????????н?????????????????????????????????????
??????????????????????У????????ж??????????????????????????????? compute(1000) ?? compute(1) ???????????к????????????????????????????????????????????????????????????е????????????????????????????????????????????????????????????????????????
????for (int i = 0; i < nLoops; i++) {
????l = compute(random.nextInt());
????}
?????????????????????????????????????????й????У??????????а????????????????????????????????????? compute ????????????????????????????????????????????????????????????????????????????????????????????£?
public void doTest() {
double l;
int nLoops = 10;
Random random = new Random();
int[] input = new int[nLoops];
for (int i = 0; i < nLoops; i++) {
input[i] = random.nextInt();
}
long then = System.currentTimeMillis();
for (int i = 0; i < nLoops; i++) {
try {
l = compute(input[i]);
} catch (IllegalArgumentException iae) {
}
}
long now = System.currentTimeMillis();
System.out.println("Elapsed time:" + (now - then));
}
?????????????????????????????????????????е?????????????????????????????????????????????????????????????????????????′???????й?????????????ж?????????????????ж??????е?Ч???????????????????????????????????????????Ч????????????е????????????????????????????????????? 1476 ????л??????ж??????????????????????????
????public double ImplSlow(int n) {
????if (n < 0) throw new IllegalArgumentException("Must be > 0");
????if (n > 1476) throw new ArithmeticException("Must be < 1476");
????return verySlowImpl(n);
????}