+ -
当前位置:首页 → 问答吧 → $row['id']的效率是$row[id]的7倍。

$row['id']的效率是$row[id]的7倍。

时间:2009-01-20

来源:互联网

$array  = array('id'=>'dd');
[php]
for($i=0;$i<=88888;$i++) {
    $array[id] = 'id';
}

for($i=0;$i<=88888;$i++) {
    $array['id'] = 'id';
}
[/php]

作者: cain   发布时间: 2009-01-20

作者: okjoyel   发布时间: 2009-04-04

学习了

作者: chd1337   发布时间: 2009-04-27

请问如何可以看出 $row['id']的效率是$row[id]的7倍呢?
请解释一下,代码我也测试了一下 没看出来

本人菜鸟。。谢谢哈

作者: chd1337   发布时间: 2009-04-27

这个好像也是编码规范的一种吧  加'' 可以减少编码中出错的几率~~
没想到居然也能提高效率学习了

作者: deepblue   发布时间: 2009-04-27

学习了..

作者: 一直等下去   发布时间: 2009-04-28

学习了

作者: 默默   发布时间: 2009-04-28

学习了

作者: akxylx   发布时间: 2009-04-28

引用
引用第3楼chd1337于2009-04-27 00:08发表的  :
请问如何可以看出 $row['id']的效率是$row[id]的7倍呢?
请解释一下,代码我也测试了一下 没看出来

本人菜鸟。。谢谢哈

作者: tabooc   发布时间: 2009-04-28

学习了

作者: 埖落埖閞   发布时间: 2009-05-08

麻烦,楼主解释下………… 本人菜鸟 呵呵

作者: tengfei6678   发布时间: 2009-05-08

恩恩,麻烦lz再配点文字说明哦

作者: slg1984   发布时间: 2009-05-08

希望张老师也能在课堂上时不时讲一下怎么写才能提高效率的写法

作者: lanbo   发布时间: 2009-05-08

应该加一个运行完的速度

作者: cyr11   发布时间: 2009-05-10

引用
引用第3楼chd1337于2009-04-27 00:08发表的  :
请问如何可以看出 $row['id']的效率是$row[id]的7倍呢?
请解释一下,代码我也测试了一下 没看出来

本人菜鸟。。谢谢哈

作者: flymouse   发布时间: 2009-05-23

学习了..  

作者: 永恒lyx   发布时间: 2009-05-23

还有这么大的区别啊?

作者: walkman20   发布时间: 2009-05-23

平时觉得不加''也行  就懒加了 看来不能偷懒了   

作者: tingfeng0311   发布时间: 2009-05-23

看来还是要处处规范

作者: wangfubao   发布时间: 2009-09-14

    

作者: newsun   发布时间: 2009-09-15

规范很重要

作者: hinet   发布时间: 2009-09-15

这段代码能测出时间。
<?php
echo "row[id]开始时间:".microtime();
for($i=0;$i<=88888;$i++) {
    $array[id] = 'id';
}
echo "<br>row[id]结束时间:".microtime();
echo "<br>row['id']开始时间:".microtime();
for($i=0;$i<=88888;$i++) {
    $array['id'] = 'id';
}
echo "<br>row['id']结束时间:".microtime();
?>

作者: chenfu12   发布时间: 2009-09-15

引用
引用第21楼chenfu12于2009-09-15 15:10发表的  :
这段代码能测出时间。
<?php
echo "row[id]开始时间:".microtime();
for($i=0;$i<=88888;$i++) {
    $array[id] = 'id';
.......

这个代码我测试了下
row[id]开始时间:0.72992300 1253000883
row[id]结束时间:0.86255900 1253000883
row['id']开始时间:0.86257900 1253000883
row['id']结束时间:0.88279500 1253000883

作者: 防潮生生世世   发布时间: 2009-09-15

上面的代码我改写后。。。。能很明显的看到结果,6倍左右,看来是真的



[php]<?php
$b=microtime();//row[id]开始时间
for($i=0;$i<=88888;$i++) {
    $array[id] = 'id';
}
$a=microtime();//row[id]结束时间
$c=$a-$b;
echo "so,row[id]:".$c."<br />";

$e=microtime();//row['id']开始时间
for($i=0;$i<=88888;$i++) {
    $array['id'] = 'id';
}
$f=microtime();//row['id']结束时间
$g=$f-$e;
echo "so,row['id']:".$g."<br />";

if($c<0||$g<0){
    echo "<meta http-equiv='refresh' content='0' />";
}else{
    echo "SO,他们差的倍数为".$c/$g."倍<br />";
}
?>
[/php]





so,row[id]:0.137852
so,row['id']:0.020842
SO,他们差的倍数为6.6141445158814倍

作者: 防潮生生世世   发布时间: 2009-09-15

楼上的厉害,完美展示

作者: xmliuxw   发布时间: 2009-10-11

引用
引用楼主cain于2009-01-20 11:01发表的 $row['id']的效率是$row[id]的7倍。 :
$array  = array('id'=>'dd');
[php]
for($i=0;$i<=88888;$i++) {
    $array[id] = 'id';
}
....... =700) window.open('http://bbs.php100.com/images/back.gif');" onload="if(this.offsetWidth>'700')this.width='700';if(this.offsetHeight>'700')this.height='700';" >

作者: dingjie   发布时间: 2009-10-12

学习咯

作者: denghai   发布时间: 2009-10-13

It's a good point but do you know why ?It's normal that without single quotation mark is slower because it is assumed to be an undefined constant and then PHP tried to find the string index.

here:

http://fr.php.net/manual/en/language.types.array.php

----------------original quote----------------------

Why is $foo[bar] wrong?

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not. But why? It is common to encounter this kind of syntax in old scripts:
<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>

This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

---------------------------------

作者: boborabit   发布时间: 2009-10-13

的确

作者: zx68555   发布时间: 2009-10-13

其实规范代码就行了

作者: yue8783   发布时间: 2009-10-14