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

    需要尝试一下吗?

  • 相关阅读:
    资金管理2
    php面试题之三——PHP网络编程(高级部分)
    运用JS设置cookie、读取cookie、删除cookie
    PHP 程序员学数据结构与算法之《栈》
    《高性能MySQL》学习笔记
    如何配置Notepad++的C_C++语言开发环境
    memcached完全剖析–1. memcached的基础
    Redis和Memcached的区别
    地区三级联动
    lwip:与tcp发送相关的选项和函数
  • 原文地址:https://www.cnblogs.com/riskyer/p/3353239.html
Copyright © 2011-2022 走看看