首先是一些一般性信息。
我的PHP运行大约1000-1500,而循环取决于数据库返回的内容。
正常执行时间约为0.3秒。那太好了:))
现在我需要使用AJAX在另一个页面上加载PHP输出,因此我开始将输出添加到$html
变量而不是仅对其进行echo
,以便可以对其进行编码到JSON格式并通过AJAX发送。
我使用$html .= some_output
。 (此任务必须与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);
?>
.=
版本(html,头部和正文将被删除,因为它将加载到另一个页面上)
<?php
$time_start = microtime(true);
session_start();
include "sqlsrv_connect.php";
$skid_id = $_POST['skid_id'];
$vendor_id = $_POST['vendor_id'];
$html = "
<div id='parent'>
";
$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'];
$html .= "
<div id='S$level2' class='section'>
<div class='sectionhead'>
$level2_descr
</div>
<div class='container_holder'>
<div id='O$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'>
";
$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;
}
$html .= "
<div class='lineholder'>
<a href='../proj_hist.pdf' class='div' target='_blank'>
<div class='project'>
";
if ($level3_doc_denominator != NULL){
$html .= "
<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>
";
}
$html .= "
</div>
<div class='project_name'>
<div class='project_text'>
$level3_doc_denominator
</div>
</div>
</a>
<div class='linkholder' >
<div id='$row_id' class='top_row' >
<div class='projectstatus' style='background: #ffffff; width: 100%;'>
</div>
";
if(!(empty($level3_head))&&empty($level3_descr)){
$html .= "
<div class='link_text level3_head' style='float: left; width: 100%; white-space: nowrap;'>
$level3_head
</div>
";
}
else {
$html .= "
<a href='../functions/load_doc.php?doc_no=$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: $level3_width%; white-space: nowrap; overflow: hidden;'>
$level3_descr
</div>
</a>
";
if($level3_flag == true){
$html .= "
<div class='flag_this'>
</div>
";
}
if ($level4_active == true){
$html .= "
<div id='$row_id_EXPAND' class='expand'>
<div class='expand_icon $row_id_EXPAND_icon'>
</div>
</div>
";
}
}
$html .= "
</div>
<div class='clear'>
</div>
";
if ($level4_active == true) {
$html .= "
<div class='$row_id_level4'>
";
foreach ($level4_array as $level4){
$level4_doc_no = $level4['doc_no'];
$level4_doc_place = $level4['doc_place'];
$level4_doc_type = $level4['doc_type'];
$level4_descr = $level4['level4_desr'];
$html .= "
<div id='$row_id_SL1' class='sub_row $row_id_EXPAND_sub'>
<div class='projectstatus' style='background: #ffffff; width: 50%;'>
</div>
<a href='../functions/load_doc.php?doc_no=$level4_doc_no&doc_place=$level4_doc_place&doc_type=$level4_doc_type' class='div' target='_blank'>
<div class='sub_link_text'>
$level4_descr
</div>
</a>
</div>
<div class='clear'>
</div>
";
}
$html .= "
</div>
";
}
$html .= "
</div>
</div>
";
$row_number++;
}
$html .= "
</div>
</div>
</div>
</div>
";
$time_end = microtime(true);
$total_time = $time_end - $time_start;
$html .= 'Total execution time in seconds: ' . $total_time;
}
$html .= "
</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);
});
";
$json = array();
$json['html'] = $html;
$json['script'] = $script;
header('Content-Type: application/json');
echo json_encode( $json );
?>
答案 0 :(得分:2)
实际上......进一步了解它,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));
输出:
Array method: 0.43333s
Array method md5: d3700cc66c04760d857e8bc9a986399c
Concat method: 0.21945s
Concat method md5: d3700cc66c04760d857e8bc9a986399c
Output buffering method: 0.22360s
Output buffering method md5: d3700cc66c04760d857e8bc9a986399c
我认为对你的问题唯一合理的解释是缺乏物理内存 - 当你做一个普通的回声(没有输出缓冲)时,你不会留在内存中 - 但是如果你连接的字符串是相对的很长,那么当你将它们存储在内存中时,你可能会达到物理内存的极限(无论你使用哪种方法)......所以可能会发生交换......?
在旧代码中的第237行
<div id="<?php echo $row_id;?>_EXPAND" class="expand">
在新代码(第250行)中成为
echo "[...]<div id='$row_id_EXPAND' class='expand'>[...]"
这意味着您正在查找名为$row_id_EXPAND
的变量 - 我猜测它并不存在,因此可能会导致对每次迭代的一个或多个错误日志执行写操作循环(你在其他地方有几个相同的错误)这可能意味着每个请求===
在错误日志中有数十甚至几十万行,写下所有数据花了很多时间。
为了避免这种问题,总是将变量封装在{}
中,即。做到:
echo "[...]<div id='{$row_id}_EXPAND' class='expand'>[...]"
答案 1 :(得分:1)
尝试使用ob_start()。 实际上并没有被我自己更快地修改,但允许你从缓冲区收集回声的结果。
使用ob_start()的示例:
ob_start();
echo("Hello there!"); //would normally get printed to the screen/output to browser
$output = ob_get_clean();
所有echo都不会起作用,因为PHP将使用不同的字符串缓冲区,其结果可以在最后加载到数组中。 ob_get_clean()
将允许echo重新开始工作。
希望这有帮助。