如果计算成本过高,我们可以将过去计算的结果缓存起来。下次就能很快提取出来。
如下方法:
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;
4万+

被折叠的 条评论
为什么被折叠?