我正在尝试提高脚本的性能。
我有一个包含3543行的海量(PHP)数组。
我在(HTML)表中输出它。
对于每一行(TR),脚本需要0.003秒。因为我有3543个记录需要:3543 * 0.003秒= 10.6秒< - 这是不行的。 请帮我改进一下:
foreach ($this->m_data as $value) {
?>
<tr>
<td><?php echo $value['nom'] ?> <?php echo $value['prenom'] ?> <?php $time_start_line = microtime(true); ?></td>
<td><?php echo dateToFR($value['ddn']) ?></td>
<td>
<span class="<?php echo $visite['type_raw'] ?>">
<?php if (!isset($value[$oms_visites[0]['display-name']][0])) : ?>
<a href="?page=visiteSuivi1&pid=<?php echo $value['id'] ?>" class="">[ Ajouter une visite ]</a>
<?php else : ?>
<a href="?page=visiteSuivi2&pid=<?php echo $value['id'] ?>" class="">[ Ajouter une visite ]</a>
<?php endif; ?>
</span>
</td>
<td>
<?php if (!isset($value[$oms_visites[0]['display-name']][0])) : ?>
<a href="?page=visiteIdentification&pid=<?php echo $value['id'] ?>">[ <?php echo dateTimeToDateFR($value['date']) ?> ]</a>
<?php elseif (isset($value[$oms_visites[0]['display-name']][0]) && !isset($value[$oms_visites[1]['display-name']][0])): ?>
<a href="?page=visiteSuivi1&pid=<?php echo $value['id'] ?>&vid=<?php echo $value[$oms_visites[0]['display-name']][0]['id'] ?>" class="">[ <?php echo dateToFr($value[$oms_visites[0]['display-name']][0]['dateVisite']) ?> ]</a>
<?php elseif (isset($value[$oms_visites[1]['display-name']][0]) && count($value[$oms_visites[1]['display-name']])): ?>
<a href="?page=visiteSuivi2&pid=<?php echo $value['id'] ?>&vid=<?php echo $value[$oms_visites[1]['display-name']][count($value[$oms_visites[1]['display-name']]) - 1]['id'] ?>" class="">[ <?php echo dateToFr($value[$oms_visites[1]['display-name']][count($value[$oms_visites[1]['display-name']]) - 1]['dateVisite']) ?> ]</a>
<?php endif; ?>
</td>
<td>
<a href="?page=visiteIdentification&pid=<?php echo $value['id'] ?>">[ <?php echo dateTimeToDateFR($value['date']) ?> ]</a>
</td>
<td class="datatable_row_detail" style="display:none">
<div class="row">
<!-- Visite Identification -->
<div class="columns large-12">
<div class="row">
<div class="columns large-4"><span class="itemDetail"><?php echo dateTimeToDateFR($value['date']) ?> </span></div>
<div class="columns large-4"> <a class="itemDetail" href="?page=visiteIdentification&pid=<?php echo $value['id'] ?>">[ Visite Identification ]</a></div>
<div class="columns large-4"><a class="itemDetail" href="#">[ Version PDF ]</a> </div>
</div>
</div>
<!-- Visite suivie 1 -->
<?php if (isset($value[$oms_visites[0]['display-name']][0])): ?>
<div class="columns large-12">
<div class="row">
<div class="columns large-4"><span class="itemDetail"><?php echo dateToFr($value[$oms_visites[0]['display-name']][0]['dateVisite']) ?></span></div>
<div class="columns large-4"><a class="itemDetail" href="?page=visiteSuivi1&pid=<?php echo $value['id'] ?>&vid=<?php echo $value[$oms_visites[0]['display-name']][0]['id'] ?>" >[ Visite <?php echo $oms_visites[0]['display-name'] ?> ]</a></div>
<div class="columns large-4"><a class="itemDetail" href="#">[ Version PDF ]</a> </div>
</div>
</div>
<?php endif; ?>
<!-- Visite suivie 2 -->
<?php if (isset($value[$oms_visites[1]['display-name']][0])): ?>
<?php foreach ($value[$oms_visites[1]['display-name']] as $key => $value_v) : ?>
<div class="columns large-12">
<div class="row">
<div class="columns large-4"><span class="itemDetail"><?php echo dateToFr($value_v['dateVisite']) ?></span></div>
<div class="columns large-4"><a class="itemDetail" href="?page=visiteSuivi2&pid=<?php echo $value['id'] ?>&vid=<?php echo $value_v['id'] ?>" >[ Visite Suivie <?php echo $key + 2 ?> ]</a></div>
<div class="columns large-4"><a class="itemDetail" href="#">[ Version PDF ]</a> </div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php
$time_end = microtime(true);
$time = $time_end - $time_start_line;
echo "$time secondes\n";
?>
</div>
</td>
</tr>
<?php
}
经过一些测试后我发现这需要0.004秒
<tr>
<td><?php echo $value['nom'] ?> <?php echo $value['prenom'] ?> </td>
<td><?php echo ($value['ddn']) ?></td>
<td>
</td>
<td>
</td>
<td>
</td>
<td class="datatable_row_detail" style="display:none">
<div class="row">
<!-- Visite Identification -->
<div class="columns large-12">
<div class="row">
</div>
</div>
</div>
</td>
</tr>
这需要4秒,唯一的区别是两个回声:
<tr>
<td><?php echo $value['nom'] ?> <?php echo $value['prenom'] ?> </td>
<td><?php echo ($value['ddn']) ?></td>
<td>
</td>
<td>
</td>
<td>
<a href="?page=visiteIdentification&pid=<?php echo $value['id'] ?>">[ <?php echo ($value['date']) ?> ]</a>
</td>
<td class="datatable_row_detail" style="display:none">
<div class="row">
<!-- Visite Identification -->
<div class="columns large-12">
<div class="row">
</div>
</div>
</div>
</td>
</tr>
现在我想知道我是否使用PHP字符串来连接TR并且只做一个回声会更快吗?
答案 0 :(得分:0)
为什么不将大量数据拆分成页面。您还可以延迟加载页面,使其显示为单个页面。没人能一次看到所有数据。
答案 1 :(得分:0)
最终我成功了!我从11秒过了0.04秒!我发现了原因:
这样做:
foreach ($this->m_data as $value) {
?>
<tr>
<td><?php echo $value['nom'] ?> <?php echo $value['prenom'] ?> <?php $time_start_line = microtime(true); ?></td>
<td><?php echo dateToFR($value['ddn']) ?></td>
<td>
<span class="<?php echo $visite['type_raw'] ?>">
<?php if (!isset($value[$oms_visites[0]['display-name']][0])) : ?>
<a href="?page=visiteSuivi1&pid=<?php echo $value['id'] ?>" class="">[ Ajouter une visite ]</a>
<?php else : ?>
<a href="?page=visiteSuivi2&pid=<?php echo $value['id'] ?>" class="">[ Ajouter une visite ]</a>
<?php endif; ?>
</span>
</td>
<td>
<?php if (!isset($value[$oms_visites[0]['display-name']][0])) : ?>
<a href="?page=visiteIdentification&pid=<?php echo $value['id'] ?>">[ <?php echo dateTimeToDateFR($value['date']) ?> ]</a>
<?php elseif (isset($value[$oms_visites[0]['display-name']][0]) && !isset($value[$oms_visites[1]['display-name']][0])): ?>
<a href="?page=visiteSuivi1&pid=<?php echo $value['id'] ?>&vid=<?php echo $value[$oms_visites[0]['display-name']][0]['id'] ?>" class="">[ <?php echo dateToFr($value[$oms_visites[0]['display-name']][0]['dateVisite']) ?> ]</a>
<?php elseif (isset($value[$oms_visites[1]['display-name']][0]) && count($value[$oms_visites[1]['display-name']])): ?>
<a href="?page=visiteSuivi2&pid=<?php echo $value['id'] ?>&vid=<?php echo $value[$oms_visites[1]['display-name']][count($value[$oms_visites[1]['display-name']]) - 1]['id'] ?>" class="">[ <?php echo dateToFr($value[$oms_visites[1]['display-name']][count($value[$oms_visites[1]['display-name']]) - 1]['dateVisite']) ?> ]</a>
<?php endif; ?>
</td>
<td>
<a href="?page=visiteIdentification&pid=<?php echo $value['id'] ?>">[ <?php echo dateTimeToDateFR($value['date']) ?> ]</a>
</td>
<td class="datatable_row_detail" style="display:none">
<div class="row">
<!-- Visite Identification -->
<div class="columns large-12">
<div class="row">
<div class="columns large-4"><span class="itemDetail"><?php echo dateTimeToDateFR($value['date']) ?> </span></div>
<div class="columns large-4"> <a class="itemDetail" href="?page=visiteIdentification&pid=<?php echo $value['id'] ?>">[ Visite Identification ]</a></div>
<div class="columns large-4"><a class="itemDetail" href="#">[ Version PDF ]</a> </div>
</div>
</div>
<!-- Visite suivie 1 -->
<?php if (isset($value[$oms_visites[0]['display-name']][0])): ?>
<div class="columns large-12">
<div class="row">
<div class="columns large-4"><span class="itemDetail"><?php echo dateToFr($value[$oms_visites[0]['display-name']][0]['dateVisite']) ?></span></div>
<div class="columns large-4"><a class="itemDetail" href="?page=visiteSuivi1&pid=<?php echo $value['id'] ?>&vid=<?php echo $value[$oms_visites[0]['display-name']][0]['id'] ?>" >[ Visite <?php echo $oms_visites[0]['display-name'] ?> ]</a></div>
<div class="columns large-4"><a class="itemDetail" href="#">[ Version PDF ]</a> </div>
</div>
</div>
<?php endif; ?>
<!-- Visite suivie 2 -->
<?php if (isset($value[$oms_visites[1]['display-name']][0])): ?>
<?php foreach ($value[$oms_visites[1]['display-name']] as $key => $value_v) : ?>
<div class="columns large-12">
<div class="row">
<div class="columns large-4"><span class="itemDetail"><?php echo dateToFr($value_v['dateVisite']) ?></span></div>
<div class="columns large-4"><a class="itemDetail" href="?page=visiteSuivi2&pid=<?php echo $value['id'] ?>&vid=<?php echo $value_v['id'] ?>" >[ Visite Suivie <?php echo $key + 2 ?> ]</a></div>
<div class="columns large-4"><a class="itemDetail" href="#">[ Version PDF ]</a> </div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php
$time_end = microtime(true);
$time = $time_end - $time_start_line;
echo "$time secondes\n";
?>
</div>
</td>
</tr>
<?php
}
在大量数据的性能方面具有成本效益。有了这个:
foreach ($this->m_data as $value) {
$outputTR .= '<tr>';
$outputTR .= '<td>' . $value['nom'] . ' ' . $value['prenom'] . '</td>';
$outputTR .= '<td>' . dateToFR($value['ddn']) . '</td>';
$outputTR .= '<td><span>';
if (!isset($value[$oms_visites[0]['display-name']][0])) {
$outputTR.='<a href="?page=visiteSuivi1&pid=' . $value['id'] . '">[ Ajouter une visite ]</a>';
} else {
$outputTR.='<a href="?page=visiteSuivi2&pid=' . $value['id'] . '">[ Ajouter une visite ]</a>';
}
$outputTR .= '</span> </td>';
$outputTR .= '<td>';
if (!isset($value[$oms_visites[0]['display-name']][0])) {
$outputTR .= '<a href="?page=visiteIdentification&pid=' . $value['id'] . '"> [' . dateTimeToDateFR($value['date']) . ']</a>';
} elseif (isset($value[$oms_visites[0]['display-name']][0]) && !isset($value[$oms_visites[1]['display-name']][0])) {
$outputTR .= '<a href="?page=visiteSuivi1&pid=' . $value['id'] . '&vid=' . $value[$oms_visites[0]['display-name']][0]['id'] . '"> [' . dateToFR($value[$oms_visites[0]['display-name']][0]['dateVisite']) . ']</a>';
} elseif (isset($value[$oms_visites[1]['display-name']][0]) && count($value[$oms_visites[1]['display-name']])) {
$outputTR .= '<a href="?page=visiteSuivi2&pid=' . $value['id'] . '&vid=' . $value[$oms_visites[1]['display-name']][count($value[$oms_visites[1]['display-name']]) - 1]['id'] . '"> [' . dateToFR($value[$oms_visites[1]['display-name']][count($value[$oms_visites[1]['display-name']]) - 1]['dateVisite']) . ']</a>';
}
$outputTR .='</td>';
$outputTR .= '<td>';
$outputTR .= '<a href="?page=visiteIdentification&pid=' . $value['id'] . '">[' . dateTimeToDateFR($value['date']) . ']</a>';
$outputTR .= '</td>';
$outputTR .= '<td class="datatable_row_detail" style="display:none">';
$outputTR .= '<div class="row">';
$outputTR .= '<div class="columns large-12">';
$outputTR .= '<div class="row">';
$outputTR .= '<div class="columns large-4"><span class="itemDetail">' . dateTimeToDateFR($value['date']) . '</span></div>';
$outputTR .= '<div class="columns large-4"> <a class="itemDetail" href="?page=visiteIdentification&pid=' . $value['id'] . '">[ Visite Identification ]</a></div>';
$outputTR .= '<div class="columns large-4"><a class="itemDetail" href="#">[ Version PDF ]</a> </div>';
$outputTR .= '</div>';
$outputTR .= '</div>';
if (isset($value[$oms_visites[0]['display-name']][0])) {
$outputTR .= '<div class="columns large-12">';
$outputTR .= '<div class="row">';
$outputTR .= '<div class="columns large-4"><span class="itemDetail">' . dateToFr($value[$oms_visites[0]['display-name']][0]['dateVisite']) . '</span></div>';
$outputTR .= '<div class="columns large-4"> <a class="itemDetail" href="?page=visiteSuivi1&pid=' . $value['id'] . '&vid=' . $value[$oms_visites[0]['display-name']][0]['id'] . '">[ Visite Suivie 1 ]</a></div>';
$outputTR .= '<div class="columns large-4"><a class="itemDetail" href="#">[ Version PDF ]</a> </div>';
$outputTR .= '</div>';
$outputTR .= '</div>';
}
if (isset($value[$oms_visites[1]['display-name']][0])) {
foreach ($value[$oms_visites[1]['display-name']] as $key => $value_v) {
$outputTR .= '<div class="columns large-12">';
$outputTR .= '<div class="row">';
$outputTR .= '<div class="columns large-4"><span class="itemDetail">' . dateToFr($value_v['dateVisite']) . '</span></div>';
$outputTR .= '<div class="columns large-4"> <a class="itemDetail" href="?page=visiteSuivi2&pid=' . $value['id'] . '&vid=' . $value_v['id'] . '">[ Visite Suivie ' . ($key + 2) . ' ]</a></div>';
$outputTR .= '<div class="columns large-4"><a class="itemDetail" href="#">[ Version PDF ]</a> </div>';
$outputTR .= '</div>';
$outputTR .= '</div>';
}
}
$outputTR .='</div>';
$outputTR .='</td>';
$outputTR .= '</tr>';
}
echo $outputTR;
我个人讨厌将PHP代码放在PHP代码中,但从性能的角度来看,结果非常壮观。 我从0.032秒/线传递到2.3 exp -5秒/线。我不太确定问题是否在于打开标签和关闭标签,还是关于多个回声。