dongqin1167 2016-08-03 00:07
浏览 90
已采纳

PHP foreach代码性能速度

I am trying to optimize increase the speed of this loop. I would like it if anyone has any ideas to boost the speed as there is thousands of members and it does take time time to complete currently

SQL Stored PROCEDURE MembersMobileByVenue returns this-> Example

FirstName Phone     Venue
Aaron   04*******   7272CD46D51F
Brad    04*******   CF105BB0
Adam    04*******   7272CD46D51F
Craig   04*******   CF105BB0

PHP

$venueIDS = isset($_POST['location']) ? $_POST['location'] : array();

$msg = $_POST['message'];
$response = array(); 

if(!empty($venueIDS)){
        $Members = GoldCardMembers::MembersMobileByVenue();
        foreach($Members as $Member){
            if(in_array($Member->Venue, $venueIDS)){
                $destination = $Member->Phone;
                $text = 'Hi ' . $Member->FirstName . ' ' .$msg. '. Reply STOP to opt out';
                $ref = 'Members';

                $content =  '&to='.rawurlencode($destination).
                            '&message='.rawurlencode($text).
                            '&ref='.rawurlencode($ref);

                $smsbroadcast_response = sendSMS($content);
                $response_lines = explode("
", $smsbroadcast_response);

                 foreach( $response_lines as $data_line){
                    $message_data = "";
                    $message_data = explode(':',$data_line);
                    if($message_data[0] == "OK"){
                        array_push($response, "The message to ".$message_data[1]." was successful, with reference ".$message_data[2]."
");
                    }elseif( $message_data[0] == "BAD" ){
                        array_push($response, "The message to ".$message_data[1]." was NOT successful. Reason: ".$message_data[2]."
");
                    }elseif( $message_data[0] == "ERROR" ){
                        array_push($response, "There was an error with this request. Reason: ".$message_data[1]."
");
                    }
                 }
            }
        }
}

foreach($response as $message){
    echo $message;
}
  • 写回答

1条回答 默认 最新

  • douluogu8713 2016-08-03 02:29
    关注
    $venueIDS = isset($_POST['location']) ? $_POST['location'] : array();
    
    $msg = $_POST['message'];
    
    $text = "";
    $destination = "";
    $content = "";
    $response_lines = array();
    $smsbroadcast_response  = "";
    $message_data = array();
    
    if(!empty($venueIDS)){
    
            $Members = GoldCardMembers::MembersMobileByVenue();
            foreach($Members as $Member){ // O(n) size of $Members
                if(in_array($Member->Venue, $venueIDS)){ // O(v) size of $venueIDs
    
                    $destination = rawurlencode($Member->Phone);
                    $text = rawurlencode($Member->FirstName . ' ' .$msg);
    
                    $content =  '&to='. $destination .
                                '&message=Hi ' . $text . '. Reply STOP to opt out'.
                                '&ref=Members';
    
                    $smsbroadcast_response = sendSMS($content);
                    $response_lines = explode("
    ", $smsbroadcast_response); // O(r) number of "
    " occurances
    
                     foreach( $response_lines as $data_line){ // O(r)
    
                        $message_data = explode(':',$data_line); // O(d) number of ":" occurances
    
                        if($message_data[0] == "OK"){
                            echo "The message to ".$message_data[1]." was successful, with reference ".$message_data[2]."
    ";
                        }elseif( $message_data[0] == "BAD" ){
                            echo "The message to ".$message_data[1]." was NOT successful. Reason: ".$message_data[2]."
    ";
                        }elseif( $message_data[0] == "ERROR" ){
                            echo "There was an error with this request. Reason: ".$message_data[1]."
    ";
                        }
                     }
                }
            }
    }
    

    Here's a list of what I did:

    • removed the for loop from bottom and echo the $message directly from the main loop instead
    • removed $ref var
    • initialized $destination, $text, $content, $response_lines, $smsbroadcast_response and $message_data vars outside the for loop since declaring them inside the loop with each iteration is sub-optimal

    That's all I can think about for now.

    Notes for further improvements:

    I'd investigate on the performance of sendSMS that you're using. It could be one of the components that are slowing things down.

    In order to dig deeper, a helpful tool for analyzing performance is big Oh notation. So I've added comments to label loop components.

    Because we're looking at multiple nested loops we generally have something like O(N^3) here.

    However, we could write that equation to be more detailed such as this:

    O( n * (v + 2r) * d)

    (please check code comments to see what each variable represents)

    Since you're asking for a high "n" (number of members) I'd investigate ways to make the other variables like "v", "r" or "d" smaller.

    Eliminating any of these variables altogether like removing

    $message_data = explode(':',$data_line);

    have the highest positive impact on performance (given that is still achieves the same functionality).

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c