如何让代码运行的更快?

代码已经编写好了,希望能够更进一步优化一下
代码最终目的是要格式化数组方便输出到前台页面的。
最终希望出来的数组格式是

Array
(
    [2017-09-17] => Array
        (
            [game_062] => Array
                (
                    [reg_num] => 30
                    [amount_num] => 1026
                    [pay_user_num] => 2
                    [login_num] => 3
                )

            [game_030] => Array
                (
                    [reg_num] => 1
                    [amount_num] => 
                    [pay_user_num] => 
                    [login_num] => 3
                )

        )

    [2017-09-18] => Array
        (
            [game_062] => Array
                (
                    [reg_num] => 2
                    [amount_num] => 880
                    [pay_user_num] => 1
                    [login_num] => 1
                )

            [game_030] => Array
                (
                    [reg_num] => 1
                    [amount_num] => 6
                    [pay_user_num] => 1
                    [login_num] => 4
                )

        )

)

代码如下:

        $from_time = I("get.date");
        if(empty($from_time)) {
            $date['a1'] = date("Y-m-d");
            $date['a2'] = date("Y-m-d");
        }else{
            $date = sep_from_date($from_time);  
        }
        $date['a2'] = $date['a1'] == $date['a2'] ? date('Y-m-d 23:59:59'): $date['a2'];

        $gameId = I("get.gameId");
        $sql = "";
        if(!empty($gameId)) {
            $sql = " AND gameId = \"".$gameId."\" ";
        }
        $game_users = M("game_users") -> query('SELECT count(id) AS reg_num,gameId,FROM_UNIXTIME(regTime,"%Y-%m-%d") AS _day FROM ad_game_users WHERE FROM_UNIXTIME(regTime,"%Y-%m-%d") >= "'.$date['a1'].'" AND FROM_UNIXTIME(regTime,"%Y-%m-%d") <= "'.$date['a2'].'" '.$sql.'GROUP BY gameId,_day');
        $game_paynotify = M("game_paynotify") -> query('SELECT sum(total_amount) AS amount_num,gameId,str_to_date(addtime,"%Y-%m-%d") AS _day FROM ad_game_paynotify WHERE `status` = 1 AND str_to_date(addtime,"%Y-%m-%d") >= "'.$date['a1'].'" AND str_to_date(addtime,"%Y-%m-%d") <= "'.$date['a2'].'" '.$sql.'GROUP BY gameId,_day');
        $game_paynotify_users = M("game_paynotify") -> query('SELECT count(DISTINCT(userId)) AS pay_user_num,gameId,str_to_date(addtime,"%Y-%m-%d") AS _day FROM ad_game_paynotify WHERE `status` = 1 AND str_to_date(addtime,"%Y-%m-%d") >= "'.$date['a1'].'" AND str_to_date(addtime,"%Y-%m-%d") <= "'.$date['a2'].'" '.$sql.'GROUP BY gameId,_day');
        $game_login_active = M("game_login_active") -> query('SELECT login_num,gameId,addtime FROM ad_game_login_active WHERE addtime >= "'.$date['a1'].'" AND addtime <= "'.$date['a2'].'" '.$sql);
        $regArr = array();
        $gameArr = array();
        foreach ($game_users as $key => $value) {
            $regArr[$value['gameId']][$value['_day']] = $value['reg_num'];
            $gameArr[$value['gameId']] = $value['gameId'];
        }
        $amountArr = array();
        foreach ($game_paynotify as $key => $value) {
            $amountArr[$value['gameId']][$value['_day']] = $value['amount_num'];
            $gameArr[$value['gameId']] = $value['gameId'];
        }
        $payUserArr = array();
        foreach ($game_paynotify_users as $key => $value) {
            $payUserArr[$value['gameId']][$value['_day']] = $value['pay_user_num'];
            $gameArr[$value['gameId']] = $value['gameId'];
        }
        $loginActive = array();
        foreach ($game_login_active as $key => $value) {
            $loginActive[$value['gameId']][$value['addtime']] = $value['login_num'];
        }
        $dt_start = strtotime($date['a1']);
        $dt_end   = strtotime($date['a2']);
        $result = array();
        $game_count = count($gameArr);
        $gameArr = array_reverse($gameArr);
        do { 
            $i = 0;
            foreach ($gameArr as $key => $value) {
                if($i > 2) {  // 限制四条,前台友显示
                    break;
                }
                $dt_date = date('Y-m-d', $dt_start);
                $result[$dt_date][$value]['reg_num'] = $regArr[$value][$dt_date];
                $result[$dt_date][$value]['amount_num'] = $amountArr[$value][$dt_date];
                $result[$dt_date][$value]['pay_user_num'] = $payUserArr[$value][$dt_date];
                $result[$dt_date][$value]['login_num'] = $loginActive[$value][$dt_date];
                $i++;
            }
        } while (($dt_start += 86400) <= $dt_end);    // 重复 Timestamp + 1 天(86400), 直至大于结束日期中止
        print_r($result);
智慧大石
浏览 525回答 4
4回答

函数式编程

选择合适的算法和数据结构 使用尽量小的数据类型
打开App,查看更多内容
随时随地看视频慕课网APP