影响PHP性能的因素

<?php

// for 冒泡排序
$array = array(32,245,435,435,13,435,88,7666,7,989,89,34,87,9,0);
$count = count($array);
$starttime = microtime(true);

for ($c=0;$c<100000;$c++) {
    for ($i=0;$i<$count;$i++) {
        for ($y=0;$y<$count-1;$y++) {
            if ($array[$y+1] < $array[$y]) {
                $t = $array[$y];
                $array[$y] = $array[$y+1];
                $array[$y+1] = $t;
            }
        }
    }
}

print_r($array);
echo microtime(true) – $starttime;
echo “\n”;

// while 冒泡排序
$c = $i = $y = 0;
$array = array(32,245,435,435,13,435,88,7666,7,989,89,34,87,9,0);
$count = count($array);
$starttime = microtime(true);

$c = 0;
while ($c<100000) {
    while ($i<$count) {
        for ($y=0;$y<$count-1;$y++) {
            if ($array[$y+1] < $array[$y]) {
                $t = $array[$y];
                $array[$y] = $array[$y+1];
                $array[$y+1] = $t;
            }
        }
        $i++;
    }
    $c++;
}

print_r($array);
echo microtime(true) – $starttime;
echo “\n”;

 

// while 冒泡排序
$c = $i = $y = 0;
$array = array(32,245,435,435,13,435,88,7666,7,989,89,34,87,9,0);
$count = count($array);
$starttime = microtime(true);

$c = 0;
while ($c<100000) {
    $i = 0;
    while ($i<$count) {
        $y = 0;
        while ($y<$count-1) {
            if ($array[$y+1] < $array[$y]) {
                $t = $array[$y];
                $array[$y] = $array[$y+1];
                $array[$y+1] = $t;
            }
            ++$y;
        }
        $i++;
    }
    $c++;
}

print_r($array);
echo microtime(true) – $starttime;
echo “\n”;

 

exit;

 

 

 

运行结果如下:

 

Array

(

[0] => 0

[1] => 7

[2] => 9

[3] => 13

[4] => 32

[5] => 34

[6] => 87

[7] => 88

[8] => 89

[9] => 245

[10] => 435

[11] => 435

[12] => 435

[13] => 989

[14] => 7666

)

6.7870390415192

Array

(

[0] => 0

[1] => 7

[2] => 9

[3] => 13

[4] => 32

[5] => 34

[6] => 87

[7] => 88

[8] => 89

[9] => 245

[10] => 435

[11] => 435

[12] => 435

[13] => 989

[14] => 7666

)

0.016165971755981

Array

(

[0] => 0

[1] => 7

[2] => 9

[3] => 13

[4] => 32

[5] => 34

[6] => 87

[7] => 88

[8] => 89

[9] => 245

[10] => 435

[11] => 435

[12] => 435

[13] => 989

[14] => 7666

)

6.8951241970062

About 智足者富

http://chenpeng.info

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>