dongxuan2015 2011-01-06 23:59
浏览 10
已采纳

如何优化此代码段以更快地执行并占用更小的空间?

We have a small script that uses memcache to track concurrent sessions, with the client making a new request every 10-seconds to "renew" its session and also get the latest count of users online.

$session    = $_GET['session'];
$streamid   = $_GET['streamid'];

if(!is_null($session) && !is_null($streamid)) {
    $memcache = new Memcache;
    $memcache->connect('localhost', 11211);

    $data   = $memcache->get($streamid);

    if($data === false) {
        $data   = array($session => time()+10);

        $memcache->add($streamid,$data,0,10);
    } else {
        $now            = time();
        $streamCount    = count($data);

        for($i=0;$i<$streamCount;$i++) {
            if($data[$i] > $now) {    
                    unset($data[$i]);
            }
        }

        $data[$session] = time()+10;
    }

    echo count($data);
} else {
    echo 'no session or stream specified';
}
  • 写回答

1条回答 默认 最新

  • duanrebo3559 2011-01-07 00:20
    关注

    Have you tried running this yet? Do you need to optimize it? Maybe it works like how you want it right now. It seems like a better solution would be to decouple it and write it as clean as possible. Put timers around calls and if you see something being slow then try to fix that code. Don't optimize if you don't need to.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效