栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

比较器工作方式的效率

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

比较器工作方式的效率

平均而言,您的排序算法将针对N个学生的数组调用

complexOperation()
大约2
N次日志的方法。如果操作真的很慢,最好为每个学生运行一次。这可以为1000名学生带来数量级的提高。

但是,您不必显式地执行此操作:您可以

complexOperation(...)
存储每个学生的结果,然后在后续请求中返回缓存的值:

private Map<Student,Integer> cache = new HashMap<Student,Integer>();private int complexOperation(Student s) {    // See if we computed the rank of the student before    Integer res = cache.get(s);    if (res != null) {        // We did! Just return the stored result:        return res.intValue();    }    ... // do the real computation here    // Save the result for future invocations    cache.put(s, result);    return result;}

请注意,为了使这种方法起作用,

Student
类需要实现
hashCode
equals



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428535.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号