• Redis需要你来做的算法优化


             阅读一个优秀的Server内核实现,早期的代码比后期的代码要好得多。因为在早期的代码里,你可以学习到一个黑客级别的程序猿到底在思考什么。同时,你能看到他哪里写得差劲,以及后来是怎么优化的。

             如果你一心追求最新的技术,但是,不关心它是怎么成长起来的,方向都走错了。走错了方向,跑得越快,离目标越远。

    /* Search the first timer to fire.
     * This operation is useful to know how many time the select can be
     * put in sleep without to delay any event.
     * If there are no timers NULL is returned.
     *
     * Note that's O(N) since time events are unsorted.
     * Possible optimizations (not needed by Redis so far, but...):
     * 1) Insert the event in order, so that the nearest is just the head.
     *    Much better but still insertion or deletion of timers is O(N).
     * 2) Use a skiplist to have this operation as O(1) and insertion as O(log(N)).
     */
    static aeTimeEvent *aeSearchNearestTimer(aeEventLoop *eventLoop)
    {
        aeTimeEvent *te = eventLoop->timeEventHead;
        aeTimeEvent *nearest = NULL;
    
        while(te) {
            if (!nearest || te->when_sec < nearest->when_sec ||
                    (te->when_sec == nearest->when_sec &&
                     te->when_ms < nearest->when_ms))
                nearest = te;
            te = te->next;
        }
        return nearest;
    }


    Redis在查找最近的时间事件的时候,采用了暴力遍历,时间复杂度是O(N)。优化的方法作者想到了2种,但是他并没有实现,或许他觉得这个不是最紧迫的事情。

    需要尝试一下吗?

  • 相关阅读:
    动态规划(下):如何求得状态转移方程并进行编程实现?
    SQL注入
    动态规划(上):如何实现基于编辑距离的查询推荐?
    XSS
    组合
    访问控制
    排列
    基于离散对数的签名方案
    基于RSA的签名方案
    centos7基于Hadoop的集群安装配置Hive
  • 原文地址:https://www.cnblogs.com/riskyer/p/3353239.html
Copyright © 2020-2023  润新知