smarty.php.net
fasttemplate
phplib
这些都可说明问题的说PS:你的template可能用的有问题
连这种都出来了,实在不该啊$dichange.="<TABLE bgColor=#000000 border=0 cellPadding=0 cellSpacing=1 width=620>
<TR>
<TD>客户姓名</TD>
<TD>性别</TD>
<TD>来访时间</TD>
<TD>查看信息</TD>
</TR>";
fasttemplate
phplib
这些都可说明问题的说PS:你的template可能用的有问题
连这种都出来了,实在不该啊$dichange.="<TABLE bgColor=#000000 border=0 cellPadding=0 cellSpacing=1 width=620>
<TR>
<TD>客户姓名</TD>
<TD>性别</TD>
<TD>来访时间</TD>
<TD>查看信息</TD>
</TR>";
这是4个测试,计时类采用pear的Bench/Iterate.php,模板类采用phplib-7.4的template.inc
为排除加载模板文件的开销,加载模板采用串赋值。
测试系统,p2 450/64m windows 98 pws4 php4.3.2
测试代码:共4个函数。
f1() 使用模板,各模板变量单独赋值,最后一次输出。
f2() 使用模板,td模板变量用数组赋值,最后一次输出。
f3() php、html混合,直接输出。
f4() php、html混合,用变量缓存结果,最后一次输出。
各函数使用同一算法用以输出本月月历
测试结果,以函数各执行100次的平均时间计算,单位:秒
f1() TrimChinese:0.071217
f2() TrimChinese:0.072104
f3() TrimChinese:0.044667
f4() TrimChinese:0.001532结果你可以自行判定。
我只知道使用模板会慢一些,绝没想到是这样一个结果。<?php
function f1() {
$str = "<table border>
<colgroup span=5 align=right></colgroup>
<colgroup span=2 align=right style='color:red'></colgroup>
<tr><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th><th>日</th></tr>
<!-- BEGIN Month -->
<tr>
<!-- BEGIN Date -->
<td bgcolor='{color}'>{data}</td>
<!-- END Date -->
</tr>
<!-- END Month -->
</table>
";
$mode = 7;
$n = date("t");
$w = date("w",mktime(0,0,0,date("m"),1,date("Y")));
$w = $w==0?7:$w;
$j = date("j");
$tpl = new template;
$tpl->halt_on_error = "report";
$tpl->set_var("hand",$str);
$tpl->set_block("hand","Month","row");
$tpl->set_block("Month","Date","col");
$d = $i = 1;
while($i <= $n) {
if($d < $w)
$tpl->set_var("data"," ");
else {
$tpl->set_var("color",($i==$j?"#0000FF":""));
$tpl->set_var("data",$i++);
}
$tpl->parse("col","Date",true);
if($d++ % $mode == 0) {
$tpl->parse("row","Month",true);
$tpl->set_var("col");
}
}
if(--$d % $mode > 0) {
while($d++ % $mode) {
$tpl->set_var("data"," ");
$tpl->parse("col","Date",true);
}
$tpl->parse("row","Month",true);
}$tpl->parse("out","hand",true);
$tpl->p(out);
}function f2() {
$str = "<table border>
<colgroup span=5 align=right></colgroup>
<colgroup span=2 align=right style='color:red'></colgroup>
<tr><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th><th>日</th></tr>
<!-- BEGIN Month -->
<tr>
<!-- BEGIN Date -->
<td bgcolor='{color}'>{data}</td>
<!-- END Date -->
</tr>
<!-- END Month -->
</table>
";
$mode = 7;
$n = date("t");
$w = date("w",mktime(0,0,0,date("m"),1,date("Y")));
$w = $w==0?7:$w;
$j = date("j");
$tpl = new template;
$tpl->halt_on_error = "report";
$tpl->set_var("hand",$str);
$tpl->set_block("hand","Month","row");
$tpl->set_block("Month","Date","col");
$d = $i = 1;
while($i <= $n) {
if($d < $w)
$tpl->set_var("data"," ");
else {
$tpl->set_var(array(color=>($i==$j?"#0000FF":""),data=>$i++));
}
$tpl->parse("col","Date",true);
if($d++ % $mode == 0) {
$tpl->parse("row","Month",true);
$tpl->set_var("col");
}
}
if(--$d % $mode > 0) {
while($d++ % $mode) {
$tpl->set_var("data"," ");
$tpl->parse("col","Date",true);
}
$tpl->parse("row","Month",true);
}$tpl->parse("out","hand",true);
$tpl->p(out);
}function f3() {
$mode = 7;
$n = date("t");
$w = date("w",mktime(0,0,0,date("m"),1,date("Y")));
$w = $w==0?7:$w;
$j = date("j");
echo "<table border>
<colgroup span=5 align=right></colgroup>
<colgroup span=2 align=right style='color:red'></colgroup>
<tr><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th><th>日</th></tr>
<tr>";
$d = $i = 1;
while($i <= $n) {
if($d < $w)
echo "<td> </td>";
else {
echo "<td bgcolor='".($i==$j?"#0000FF":"")."'>".($i++)."</td>";
}
if($d++ % $mode == 0) {
echo "</tr><tr>";
}
}
if(--$d % $mode > 0) {
while($d++ % $mode) {
echo "<td> </td>";
}
echo "</tr>";
}echo "</table>";
}function f4() {
$mode = 7;
$n = date("t");
$w = date("w",mktime(0,0,0,date("m"),1,date("Y")));
$w = $w==0?7:$w;
$j = date("j");
$str = "<table border>
<colgroup span=5 align=right></colgroup>
<colgroup span=2 align=right style='color:red'></colgroup>
<tr><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th><th>日</th></tr>
<tr>";
$d = $i = 1;
while($i <= $n) {
if($d < $w)
$str .= "<td> </td>";
else {
$str .= "<td bgcolor='".($i==$j?"#0000FF":"")."'>".($i++)."</td>";
}
if($d++ % $mode == 0) {
$str .= "</tr><tr>";
}
}
if(--$d % $mode > 0) {
while($d++ % $mode) {
$str .= "<td> </td>";
}
$str .= "</tr>";
}$str .= "</table>";
}
//require_once "template.php";
require_once 'Bench/Iterate.php';$bench1 = new Bench_Iterate;
$bench1->run(100, 'f1');
$bench2 = new Bench_Iterate;
$bench2->run(100, 'f2');
$bench3 = new Bench_Iterate;
$bench3->run(100, 'f3');
$bench4 = new Bench_Iterate;
$bench4->run(100, 'f4');
$result = $bench1->get();
echo 'f1() TrimChinese:'.$result[mean]."<br>";
$result = $bench2->get();
echo 'f2() TrimChinese:'.$result[mean]."<br>";
$result = $bench3->get();
echo 'f3() TrimChinese:'.$result[mean]."<br>";
$result = $bench4->get();
echo 'f4() TrimChinese:'.$result[mean]."<br>";
?>
因为他的代码可以配合网页设计工具(如DW)进行可视化设计界的。代码运行将来的效果,直观多了。
而你的代码看起来都累。以后维护时要改变输出都麻烦(我维护过这样的代码)
2、两者都不是通常讲的模板技术。用模板就是界面尽可能与程序分开。好处,美工和程序员可以更好地做本职工作,相同之间的必要交流可以少很多。
一般要你在程序读取HTML文件中设置的参数来交互的。
3、你的代码:
$dichange.="<TD>".$sql[1]."</TD>";
$dichange.="<TD>".$sql[2]."</TD>";
你的数据库的取值不要用索引数值。这样以后修改数据表你的程序就很可能要修改。别人也不好读懂。(你在以后也一样会忘的)
4、数量比较多的<??>,感觉性能会降低很多,实际运行情况是,没感觉出来。
Bench/Iterate.php
我来测试一下。
我发现你所列的测试文件中犯了一个很大的错误:f4() 未输出结果!一个echo涉及到PHP跟WEBSEVER,WEBSERVER跟浏览器之间的通信,有没有echo,性能的差异性可想而知了。
再则我认为echo不会立即输出结果的,肯定设置了一个缓出缓冲区的。这样的话,就没必要先存在一个变量里了。直接输出即可。
但结果并没有质的变化
f1() TrimChinese:0.071436
f2() TrimChinese:0.073361
f3() TrimChinese:0.069446
f4() TrimChinese:0.020406Bench/Iterate.php在php/pear中,自己写一个也不难。另外,echo 是立即输出的,这一点是不争的事实。
微软的系统就是你所谓的“重复利用前人的成果”吧?从vwin3的5m(虽然还算不上操作系统)到win95的12m到win98的22m到win2000的280m....
不要忘记这期间硬件的性能提高了上千倍!
第一次:
f3() TrimChinese:1.22622609138
f4() TrimChinese:1.24362099171
第二次:
f3() TrimChinese:0.975206017494
f4() TrimChinese:0.943042993546
第三次:
f3() TrimChinese:1.32042789459
f4() TrimChinese:0.933300971985产生的文件有1.85M
。
。
。
//require_once "template.php";
//require_once 'Bench/Iterate.php';
function run($p1,$p2)
{
$s=microtime();
for($i=0;$i<$p1;$i++)
$p2();
$e=microtime();
//返回的时间格式为:0.07600400 1063270905
$s=substr($s,11,12).substr($s,1,9);
$e=substr($e,11,12).substr($e,1,9);
return $e-$s;
}
//$rs1=run(100, 'f1');
//$rs2=run(100, 'f2');
//f3() TrimChinese:0.07600400 1063270905:0.66673800 1063270905
//f4() TrimChinese:0.66680000 1063270905:0.43966500 1063270906
$rs3=run(1000, 'f3');
echo "<br><hr>fun4();<br>";
$rs4=run(1000, 'f4');
//echo 'f1() TrimChinese:'.$rs1."<br>";
//echo 'f2() TrimChinese:'.$rs2."<br>";
echo 'f3() TrimChinese:'.$rs3."<br>";
echo 'f4() TrimChinese:'.$rs4."<br>";
php的对象特征不是很明显,不像应用程序那样事件频繁
何况现在的php对象支持很有限,要用对象用jsp去,争到最后还是没结果
模板编程肯定要用到include ,大家都知道打开一个文件需要时间,访问量大的会造成
频繁的读写磁盘,我想对硬盘的老化不无影响吧。
如果你的公司是一个很系统的公司,项目管理都非常的严格规范,那么就用模板面向对象
公司比较小,项目不多但是很杂,那就怎么快则么开发
All roads lead to Rome
再次调用的时候我怀疑已经缓存了,或者直接用class文件不需要重新解释
故在访问量大的时候也不会造成很大的瓶颈。而php的类每次访问请求都要解释一遍,没有预编译,也没有缓存,从头到脚web服务的特征是访问的页面比较集中(比如首页的访问量最大),需要缓存的支持
那些高手写的程序都注意了这些问题,当然还有很多影响速度的方面,不赞成fnaps(blue) 兄所说 根本没必要争这十个人 ,积少成多,当注意这些问题的时间久了就成习惯成经验了