如果计算成本过高,我们可以将过去计算的结果缓存起来。下次就能很快提取出来。
如下方法:
result = cache.get(n);
if(result == null){
result = computeResult(n);
cache.put(n,result);
}
return result;
如果计算成本过高,我们可以将过去计算的结果缓存起来。下次就能很快提取出来。
如下方法:
result = cache.get(n);
if(result == null){
result = computeResult(n);
cache.put(n,result);
}
return result;