将while输出字符串添加到变量时,PHP性能会下降
首先是一些一般信息 我的PHP运行大约1000-1500次,而循环取决于数据库返回的内容 正常执行时间约为0.3秒。太好了:) 现在,我需要使用AJAX在另一个页面上加载PHP输出,因此我开始将输出添加到将while输出字符串添加到变量时,PHP性能会下降,php,ajax,Php,Ajax,首先是一些一般信息 我的PHP运行大约1000-1500次,而循环取决于数据库返回的内容 正常执行时间约为0.3秒。太好了:) 现在,我需要使用AJAX在另一个页面上加载PHP输出,因此我开始将输出添加到$html变量中,而不是仅仅在其上执行echo,以便将其编码为JSON格式并通过AJAX发送 为此,我使用$html.=some\u输出。(此任务的执行次数必须与while循环的运行次数相同) 但是,在添加$html.=而不是echo之后,脚本现在需要+9秒才能完成 对于等待AJAX调用返回结果
$html
变量中,而不是仅仅在其上执行echo
,以便将其编码为JSON格式并通过AJAX发送
为此,我使用$html.=some\u输出。(此任务的执行次数必须与while循环的运行次数相同)
但是,在添加$html.=
而不是echo
之后,脚本现在需要+9秒才能完成
对于等待AJAX调用返回结果的最终用户来说,这显然不是最优的
我在代码上做了一些microtime
,以确定问题的根源——毫无疑问,是=
操作符
有没有关于如何减少这种情况的建议
编辑:下面是代码块
首先是一个完美的加载
<?php
$time_start = microtime(true);
session_start();
include "../functions/sqlsrv_connect.php";
$skid_id = $_GET['skid_id'];
$vendor_id = $_GET['vendor_id'];
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<link rel="stylesheet" type="text/css" href="../stylesheets/fonts.css">
<link rel="stylesheet" type="text/css" href="../stylesheets/linkcontainer.css">
<script src="/js/jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="parent" style="display: none;">
<?php
$sql_get_vdm_sections = "
SELECT
ml2.level2_descr,
ml2.level2_id
FROM
main_vdm_level2 AS ml2
JOIN
vdm_index AS vdm_i
ON
vdm_i.level2_id = ml2.level2_id
WHERE
vdm_i.skid_id = $skid_id
AND
vdm_i.vendor_id = $vendor_id
GROUP BY
ml2.level2_descr,
ml2.level2_id
ORDER BY
ml2.level2_id
";
$get_vdm_sections = sqlsrv_query($sqlsrv, $sql_get_vdm_sections);
while($vdm_section = sqlsrv_fetch_array($get_vdm_sections,SQLSRV_FETCH_ASSOC)){
$level2_id = $vdm_section['level2_id'];
$level2_descr = $vdm_section['level2_descr'];
?>
<div id="S<?php echo $level2_id;?>" class="section">
<div class="sectionhead">
<?php echo $level2_descr;?>
</div>
<div class="container_holder">
<div id="O<?php echo $level2_id;?>" class="obsolete_section">
<div class="obsolete_lineholder">
<div class="obsolete_header">
<div class="clmn_header">
<p class="small bold">Obsolete Documentation</p>
</div>
</div>
</div>
<div class="obsolete_lineholder">
<div class="obsolete_linkholder">
<a href="#" class="div" target="_blank">
<div class="obsolete_text">
</div>
</a>
</div>
</div>
<div class="obsolete_lineholder">
<div class="obsolete_linkholder">
<a href="#" class="div" target="_blank">
<div class="obsolete_text">
</div>
</a>
</div>
</div>
<div class="obsolete_lineholder">
<div class="obsolete_linkholder">
<a href="#" class="div" target="_blank">
<div class="obsolete_text">
</div>
</a>
</div>
</div>
</div>
<div class="active_cnt">
<div class="button_holder">
<div class="active_btn">
<p class="small">Active Documentation</p>
</div>
<div class="obsolete_btn">
<p class="small">View Obsolete Documentation</p>
</div>
</div>
<div class="lineholder">
<div class="linkholder">
<div class="clmn_header">
<p class="small bold">Chapters</p>
</div>
</div>
</div>
<div class="linkholder_cnt">
<?php
$file_verification = true;
$sql_get_section_content = "
SELECT
file_verification,
level3_head,
level3_descr,
level4_descr,
doc_no,
doc_place,
doc_denominator,
doc_type
FROM
vdm_index
WHERE
skid_id = $skid_id
AND
vendor_id = $vendor_id
AND
level2_id = $level2_id
AND
level4_descr IS NULL
";
$get_section_content = sqlsrv_query($sqlsrv, $sql_get_section_content);
$row_number = 1;
while ($section_content = sqlsrv_fetch_array($get_section_content,SQLSRV_FETCH_ASSOC)){
$level3_file_verification = $section_content['file_verification'];
$level3_head = $section_content['level3_head'];
$level3_descr = $section_content['level3_descr'];
$level3_doc_no = $section_content['doc_no'];
$level3_doc_place = $section_content['doc_place'];
$level3_doc_denominator = $section_content['doc_denominator'];
$level3_doc_type = $section_content['doc_type'];
$level3_width = 100;
$row_id = 'S'.$level2_id.'_R'.$row_number;
$sql_get_level4 = "
SELECT
file_verification,
level4_descr,
doc_no,
doc_place,
doc_denominator,
doc_type
FROM
vdm_index
WHERE
skid_id = $skid_id
AND
vendor_id = $vendor_id
AND
level2_id = $level2_id
AND
level3_descr = '$level3_descr'
AND
level4_descr IS NOT NULL
";
$get_level4 = sqlsrv_query($sqlsrv, $sql_get_level4);
$level4_array = array();
while ($level4 = sqlsrv_fetch_array($get_level4,SQLSRV_FETCH_ASSOC)){
array_push($level4_array, $level4);
}
if (!empty($level4_array)){
$level3_width -= 10;
$level4_active = true;
}
else {
$level4_active = false;
}
if ($file_verification != true){
$level3_width -= 10;
$level3_flag = true;
}
else {
$level3_flag = false;
}
?>
<div class="lineholder">
<a href="../proj_hist.pdf" class="div" target="_blank">
<div class="project">
<?php
if ($level3_doc_denominator != NULL){
?>
<svg x="0px" y="0px" width="45px" height="100%" viewBox="0 0 45 25" preserveAspectRatio="none">
<rect fill="#66FECB" width="27.1" height="25"></rect>
<polygon fill="#66FECB" points="45,12.5 27,0.000 27,25.000 "></polygon>
</svg>
<?php
}
?>
</div>
<div class="project_name">
<div class="project_text">
<?php echo $level3_doc_denominator;?>
</div>
</div>
</a>
<div class="linkholder" >
<div id="<?php echo $row_id;?>" class="top_row" >
<div class="projectstatus" style="background: #ffffff; width: 100%;">
</div>
<?php
if(!(empty($level3_head))&&empty($level3_descr)){
?>
<div class="link_text level3_head" style="float: left; width: 100%; white-space: nowrap;">
<?php echo $level3_head;?>
</div>
<?php
}
else {
?>
<a href="../functions/load_doc.php?doc_no=<?php echo $level3_doc_no.'&doc_place='.$level3_doc_place.'&doc_type='.$level3_doc_type.'&doc_denominator='.$level3_doc_denominator;?>" class="div" target="_blank">
<div class="link_text" style="float: left; width: <?php echo $level3_width;?>%; white-space: nowrap; overflow: hidden;">
<?php echo $level3_descr;?>
</div>
</a>
<?php
if($level3_flag == true){
?>
<div class="flag_this">
</div>
<?php
}
if ($level4_active == true){
?>
<div id="<?php echo $row_id;?>_EXPAND" class="expand">
<div class="expand_icon <?php echo $row_id;?>_EXPAND_icon">
</div>
</div>
<?php }
}
?>
</div>
<div class="clear">
</div>
<?php
if ($level4_active == true) {
?>
<div class="<?php echo $row_id;?>_level4">
<?php
foreach ($level4_array as $level4){
$level4_doc_no = $level4['doc_no'];
$level4_doc_place = $level4['doc_place'];
$level4_doc_type = $level4['doc_type'];
?>
<div id="<?php echo $row_id;?>_SL1" class="sub_row <?php echo $row_id;?>_EXPAND_sub">
<div class="projectstatus" style="background: #ffffff; width: 50%;">
</div>
<a href="../functions/load_doc.php?doc_no=<?php echo $level4_doc_no.'&doc_place='.$level4_doc_place.'&doc_type='.$level4_doc_type;?>" class="div" target="_blank">
<div class="sub_link_text">
<?php echo $level4['level4_descr'];?>
</div>
</a>
</div>
<div class="clear">
</div>
<?php
}
?>
</div>
<?php
}
?>
</div>
</div>
<?php
$row_number++;
}
?>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
<div id="additional_box">
<div id="additional_info">
I hold additional information about this document!
</div>
<div id="close_additional" onclick="close_additional()">
</div>
</div>
<script>
var mouseX;
var mouseY;
$(document).mousemove( function(e) {
mouseX = e.pageX;
mouseY = e.pageY;
});
function additional() {
mouseY -= 5;
mouseX += 20;
$('#additional_box').css({'top':mouseY,'left':mouseX}).fadeIn('slow');
};
function close_additional() {
$('#additional_box').hide();
};
function reset_expansion_icon(){
var index;
var i = document.getElementsByClassName('expand_icon');
for (index =0; index < i.length; ++index) {
i[index].style.backgroundImage = "url('../img/expand.png')";
}
}
function reset_this_expansion_icon(me){
var index;
var e = $(me).children('.expand_icon')
for (index =0; index < e.length; ++index) {
e[index].style.backgroundImage = "url('../img/expand.png')";
}
}
$('.flag_this').click(function(){
additional();
});
$('.obsolete_btn').click(function() {
$('.section').hide();
var section = $(this).closest('.section');
$(section).show();
//var btn_parent = $(this).parent();
var container = $(this).closest('.container_holder');
var obsolete = $(container).children('.obsolete_section');
$(obsolete).fadeIn(500);
$('.sub_row').hide();
reset_expansion_icon();
});
$('.active_btn').click(function() {
$('.section').fadeIn(500);
$('.obsolete_section').hide();
$('.sub_row').hide();
reset_expansion_icon();
});
$('.expand').click(function() {
var parent = $(this).parent('.top_row');
var level4_class = $(parent).attr('id') + '_level4';
var sub_row_class = $(parent).attr('id') + '_EXPAND_sub';
var index;
var e = document.getElementsByClassName(sub_row_class);
var d = document.getElementsByClassName(level4_class);
for (index = 0; index < e.length; ++index) {
if(e[index].style.display == 'block'){
$(d[0]).slideUp('slow');
e[index].style.display = 'none';
}
else{
d[0].style.display = 'block';
$(e[index]).fadeIn('slow');
}
}
var expand_icon_class = $(parent).attr('id') + '_EXPAND_icon';
var i = document.getElementsByClassName(expand_icon_class);
if (e[0].style.display == 'block')
i[0].style.backgroundImage = "url('../img/condense.png')";
else
var me = $(this);
reset_this_expansion_icon(me);
});
$(window).load(function(){
$('#parent').fadeIn(300);
});
</script>
</body>
</html>
<?php
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start);
?>
也许可以试试看。
事实上,我自己并没有证明速度更快,但允许您从缓冲区收集echo的结果
使用ob_start()的示例:
由于PHP将使用另一个字符串缓冲区,其结果可以在最后加载到数组中,因此echo不会工作。ob\u get\u clean()
将允许echo重新开始工作
希望这能有所帮助。试试ob_start()也许吧。
事实上,我自己并没有证明速度更快,但允许您从缓冲区收集echo的结果
使用ob_start()的示例:
由于PHP将使用另一个字符串缓冲区,其结果可以在最后加载到数组中,因此echo不会工作。ob\u get\u clean()
将允许echo重新开始工作
希望这能有所帮助。试试ob_start()也许吧。
事实上,我自己并没有证明速度更快,但允许您从缓冲区收集echo的结果
使用ob_start()的示例:
由于PHP将使用另一个字符串缓冲区,其结果可以在最后加载到数组中,因此echo不会工作。ob\u get\u clean()
将允许echo重新开始工作
希望这能有所帮助。试试ob_start()也许吧。
事实上,我自己并没有证明速度更快,但允许您从缓冲区收集echo的结果
使用ob_start()的示例:
由于PHP将使用另一个字符串缓冲区,其结果可以在最后加载到数组中,因此echo不会工作。ob\u get\u clean()
将允许echo重新开始工作
希望这能有所帮助。首先是一些基本的基准测试
实际上。。。再仔细研究一下,concat操作符应该比数组方法(通常更方便)更快
<?php
ini_set('memory_limit', '256M');
$start = microtime(true);
$html = array();
for ($i=0; $i < 900000; $i++) {
$html[] = "Line number $i\n";
}
$html_out = implode('', $html);
$time_spent = microtime(true) - $start;
printf("Array method: %ss\n", number_format($time_spent, 5));
printf("Array method md5: %s\n", md5($html_out));
unset($start);unset($html);unset($html_out);
$start = microtime(true);
$html = '';
for ($i=0; $i < 900000; $i++) {
$html .= "Line number $i\n";
}
$time_spent = microtime(true) - $start;
printf("Concat method: %ss\n", number_format($time_spent, 5));
printf("Concat method md5: %s\n", md5($html));
unset($start);unset($html);unset($html_out);
$start = microtime(true);
ob_start();
for ($i=0; $i < 900000; $i++) {
echo "Line number $i\n";
}
$html = ob_get_clean();
$time_spent = microtime(true) - $start;
printf("Output buffering method: %ss\n", number_format($time_spent, 5));
printf("Output buffering method md5: %s\n", md5($html));
…然后试着回答这个问题
可能原因#1
我认为对您的问题唯一合理的解释是缺少物理内存-当您执行普通回显(没有输出缓冲)时,您在内存中保留的内容不多-但是如果您连接的字符串相对较长,那么当您将它们存储在内存中时,您可能会达到物理内存的限制(无论您使用哪种方法进行交换)…是否会发生交换
可能原因#2
在第237行的旧代码中
<div id="<?php echo $row_id;?>_EXPAND" class="expand">
首先是一些基本的基准测试
事实上…再深入一点,concat操作符应该比数组方法(通常更方便)更快
<?php
ini_set('memory_limit', '256M');
$start = microtime(true);
$html = array();
for ($i=0; $i < 900000; $i++) {
$html[] = "Line number $i\n";
}
$html_out = implode('', $html);
$time_spent = microtime(true) - $start;
printf("Array method: %ss\n", number_format($time_spent, 5));
printf("Array method md5: %s\n", md5($html_out));
unset($start);unset($html);unset($html_out);
$start = microtime(true);
$html = '';
for ($i=0; $i < 900000; $i++) {
$html .= "Line number $i\n";
}
$time_spent = microtime(true) - $start;
printf("Concat method: %ss\n", number_format($time_spent, 5));
printf("Concat method md5: %s\n", md5($html));
unset($start);unset($html);unset($html_out);
$start = microtime(true);
ob_start();
for ($i=0; $i < 900000; $i++) {
echo "Line number $i\n";
}
$html = ob_get_clean();
$time_spent = microtime(true) - $start;
printf("Output buffering method: %ss\n", number_format($time_spent, 5));
printf("Output buffering method md5: %s\n", md5($html));
…然后试着回答这个问题
可能原因#1
我认为对您的问题唯一合理的解释是缺少物理内存-当您执行普通回显(没有输出缓冲)时,您在内存中保留的内容不多-但是如果您连接的字符串相对较长,那么当您将它们存储在内存中时,您可能会达到物理内存的限制(无论您使用哪种方法进行交换)…是否会发生交换
可能原因#2
在第237行的旧代码中
<div id="<?php echo $row_id;?>_EXPAND" class="expand">
首先是一些基本的基准测试
事实上…再深入一点,concat操作符应该比数组方法(通常更方便)更快
<?php
ini_set('memory_limit', '256M');
$start = microtime(true);
$html = array();
for ($i=0; $i < 900000; $i++) {
$html[] = "Line number $i\n";
}
$html_out = implode('', $html);
$time_spent = microtime(true) - $start;
printf("Array method: %ss\n", number_format($time_spent, 5));
printf("Array method md5: %s\n", md5($html_out));
unset($start);unset($html);unset($html_out);
$start = microtime(true);
$html = '';
for ($i=0; $i < 900000; $i++) {
$html .= "Line number $i\n";
}
$time_spent = microtime(true) - $start;
printf("Concat method: %ss\n", number_format($time_spent, 5));
printf("Concat method md5: %s\n", md5($html));
unset($start);unset($html);unset($html_out);
$start = microtime(true);
ob_start();
for ($i=0; $i < 900000; $i++) {
echo "Line number $i\n";
}
$html = ob_get_clean();
$time_spent = microtime(true) - $start;
printf("Output buffering method: %ss\n", number_format($time_spent, 5));
printf("Output buffering method md5: %s\n", md5($html));
…然后试着回答这个问题
可能原因#1
我认为对您的问题唯一合理的解释是缺少物理内存-当您执行普通回显(没有输出缓冲)时,您在内存中保留的内容不多-但是如果您连接的字符串相对较长,那么当您将它们存储在内存中时,您可能会达到物理内存的限制(无论您使用哪种方法进行交换)…是否会发生交换
可能原因#2
在第237行的旧代码中
<div id="<?php echo $row_id;?>_EXPAND" class="expand">
首先是一些基本的基准测试
事实上…再深入一点,concat操作符应该比数组方法(通常更方便)更快
<?php
ini_set('memory_limit', '256M');
$start = microtime(true);
$html = array();
for ($i=0; $i < 900000; $i++) {
$html[] = "Line number $i\n";
}
$html_out = implode('', $html);
$time_spent = microtime(true) - $start;
printf("Array method: %ss\n", number_format($time_spent, 5));
printf("Array method md5: %s\n", md5($html_out));
unset($start);unset($html);unset($html_out);
$start = microtime(true);
$html = '';
for ($i=0; $i < 900000; $i++) {
$html .= "Line number $i\n";
}
$time_spent = microtime(true) - $start;
printf("Concat method: %ss\n", number_format($time_spent, 5));
printf("Concat method md5: %s\n", md5($html));
unset($start);unset($html);unset($html_out);
$start = microtime(true);
ob_start();
for ($i=0; $i < 900000; $i++) {
echo "Line number $i\n";
}
$html = ob_get_clean();
$time_spent = microtime(true) - $start;
printf("Output buffering method: %ss\n", number_format($time_spent, 5));
printf("Output buffering method md5: %s\n", md5($html));
…然后试着回答这个问题
可能原因#1
我认为对您的问题唯一合理的解释是缺少物理内存-当您执行普通回显(没有输出缓冲)时,您在内存中保留的内容不多-但是如果您连接的字符串相对较长,那么当您将它们存储在内存中时,您可能会达到物理内存的限制(无论您使用哪种方法进行交换)…是否会发生交换
可能原因#2
在第237行的旧代码中
<div id="<?php echo $row_id;?>_EXPAND" class="expand">
是的,将输出放在一个数组中,最后做echo json_encode($myArray);
…或者简单地echo introde('''.$myArray);
如果你想要一个字符串。请包含你的代码,这样我们可以帮助确定你的瓶颈在哪里。我还假设它是1000-1500 while循环?不是1.0-1.5(请使用美国数字格式)这太多了。1000-1500 while loops是正确的数字-但只要回显结果就没有问题了。您想要全部500行代码吗?必须尝试数组方法:)是将输出放入数组中