我正在尝试优化增加此循环的速度。如果有人有任何提高速度的想法,我想要它,因为有成千上万的会员,目前需要时间才能完成
SQL Stored PROCEDURE MembersMobileByVenue返回this-> 示例
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("\n", $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]."\n");
}elseif( $message_data[0] == "BAD" ){
array_push($response, "The message to ".$message_data[1]." was NOT successful. Reason: ".$message_data[2]."\n");
}elseif( $message_data[0] == "ERROR" ){
array_push($response, "There was an error with this request. Reason: ".$message_data[1]."\n");
}
}
}
}
}
foreach($response as $message){
echo $message;
}
答案 0 :(得分:1)
$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("\n", $smsbroadcast_response); // O(r) number of "\n" 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]."\n";
}elseif( $message_data[0] == "BAD" ){
echo "The message to ".$message_data[1]." was NOT successful. Reason: ".$message_data[2]."\n";
}elseif( $message_data[0] == "ERROR" ){
echo "There was an error with this request. Reason: ".$message_data[1]."\n";
}
}
}
}
}
以下列出了我所做的事情:
for
循环,直接从主循环移除echo
$message
$ref
var $destination
循环以来初始化$text
,$content
,$response_lines
,$smsbroadcast_response
,$message_data
和for
个vars它们在循环内部,每次迭代都是次优的这就是我现在所能想到的一切。
有关进一步改进的说明:
我会调查您正在使用的sendSMS
的效果。它可能是减慢速度的组件之一。
为了深入挖掘,分析性能的有用工具是big Oh notation。所以我在标签循环组件中添加了注释。
因为我们正在查看多个嵌套循环,所以我们通常在这里有类似O(N ^ 3)的东西。
但是,我们可以将这个等式写成更详细的,例如:
O(n *(v + 2r)* d)
(请检查代码注释以查看每个变量代表什么)
由于你要求高“n”(成员数量),我会研究如何使其他变量如“v”,“r”或“d”更小。
完全消除这些变量,如删除
$message_data = explode(':',$data_line);
对性能产生最大的积极影响(假设仍然可以实现相同的功能)。