zoukankan      html  css  js  c++  java
  • 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种,但是他并没有实现,或许他觉得这个不是最紧迫的事情。

    需要尝试一下吗?

  • 相关阅读:
    各操作系统各文件系统支持的最大文件的大小
    Java调用百度地图API
    Java面试宝典(3)Java基础部分
    Java7中的try-with-resources
    Spring学习笔记(6)——IoC的三种注入方式
    cmd中java的编译命令——java和javac、javap
    Spring学习笔记(14)——注解零配置
    java中多种方式解析xml
    双三次插值
    RCNN到faster RCNN 简介
  • 原文地址:https://www.cnblogs.com/riskyer/p/3353239.html
Copyright © 2011-2022 走看看