您的位置:首页 > 编程语言 > PHP开发

php5.2与5.3性能测试对比

2012-09-28 14:13 405 查看
源地址:/article/6442116.html

在递归测试、数值运算测试、字符串操作测试、 类和对象测试上5.3性能明显高于5.2,提升大约15%-40%,不过在内存消耗上5.3大于5.2, IO操作速度基本相同。老大伟志让我做的测试,测试方式也许不是绝对正确还希望大家多多指教,详细代码及测试结果如下:

测试环境

A. 虚拟机 + nginx0.7.67 + php5.2.10

B. 虚拟机 + nginx0.7.67 + php5.3.6

1.测试递归函数

采用交换排序算法对1万个数值元素的数组排序30次,此测试主要测试5.2与5.3在递归调用方面的性能。
<?php
function getMsecTime()
{
$arr = explode( ' ', microtime() );
return $arr[0] + $arr[1];
}

//开始时间
$startTime = getMsecTime();

/**
* 快序排序算法
*
* @param   array   $arr
* @return  array
*/
function quickSort( $arr )
{
if( count( $arr ) > 1 )
{
$key = $arr[0];
$min = array( );
$max = array( );
for( $i = 1; $i < count( $arr ); $i++ )
{
if( $arr[$i] > $key )
{
$max[] = $arr[$i];
}
else
{
$min[] = $arr[$i];
}
}
$min = count( $min ) > 0 ? quickSort( $min ) : $min;
$max = count( $max ) > 0 ? quickSort( $max ) : $max;
return array_merge( $min, array( $key ), $max );
}
else
{
return $arr;
}
}

//程序运行时间
$runTimeNum = 0;
$runTime = 0;
for( $i = 0; $i < 30; $i++ )
{
$arr = range( 1, 10000 );
shuffle( $arr );
$arr = quickSort( $arr );
$runTimeNum++;
}
$runTime = ( getMsecTime() - $startTime ) / $runTimeNum;

echo 'Running time:' , $runTime , '<br>';
echo 'Memory usage:' . memory_get_usage();
?>


测试结果

php5.2.10

Running time:0.2008661031723

Memory usage:1540616

php5.3.6

Running time:0.17402129968007

Memory usage:2134104

2.测试数值计算

对指定范围数值做循环加减法操作.
<?php
function getMsecTime()
{
$arr = explode( ' ', microtime() );
return $arr[0] + $arr[1];
}

//开始时间
$startTime = getMsecTime();

$numArr = array( 100 , -100 , 1000 , -1000 , 1 );

//程序运行时间
$runTime = 0;
$k = 0;
$j = 0;
while( $j < 1000000 )
{
$j++;
$k += $numArr[$j % 5];
}
$runTime = getMsecTime() - $startTime;

echo 'Running time:' , $runTime , '<br>';
echo 'Memory usage:' . memory_get_usage();


测试结果

php5.2.10

Running time:0.15911817550659

Memory usage:81240

php5.3.6

Running time:0.13838791847229

Memory usage:625640

3.字符串运算

对字符串做一百万次拼接操作。
<?php
function getMsecTime()
{
$arr = explode( ' ', microtime() );
return $arr[0] + $arr[1];
}

//开始时间
$startTime = getMsecTime();

//程序运行时间
$runTime = 0;

$j = 0;
$str = '';
while( $j < 1000000 )
{
$j++;
$str .= $j . 'abcdefg';
}
$runTime = getMsecTime() - $startTime;

echo 'Running time:' , $runTime , '<br>';
echo 'Memory usage:' . memory_get_usage();


测试结果

php5.2.10

Running time:0.30056118965149

Memory usage:12968168

php5.3.6

Running time:0.27767395973206

Memory usage:13512760

4.类和对象速度测试

实例化一百万次指定类并调用其类方法。

<?php
function getMsecTime()
{
$arr = explode( ' ', microtime() );
return $arr[0] + $arr[1];
}

//开始时间
$startTime = getMsecTime();

class testClass
{
public $testA;

public $testB;

public function  __construct( $num )
{
$this->testA = $num;
}

public function getNum()
{
return $this->testA;
}
};

//程序运行时间
$runTime = 0;

$j = 0;
$str = '';
while( $j < 1000000 )
{
$j++;

$a = new testClass( $j );
$a->getNum();
}
$runTime = getMsecTime() - $startTime;

echo 'Running time:' , $runTime , '<br>';
echo 'Memory usage:' . memory_get_usage();


5.文件IO操作速度测试

测试建立、删除、检测文件和文件夹及向文件内写入数据。
<?php
function getMsecTime()
{
$arr = explode( ' ', microtime() );
return $arr[0] + $arr[1];
}

//开始时间
$startTime = getMsecTime();

//程序运行时间
$runTime = 0;

$j = 0;
while( $j < 1000 )
{
$j++;
$fileName = './' . $j . 'test.txt';
if( file_exists( $fileName ) )
{
unlink( $fileName );
}
file_put_contents( $fileName , $j );

$fileDir = './' . $j . 'fileDir';
if( is_dir( $fileDir ) && file_exists( $fileDir ) )
{
rmdir( $fileDir );
}
mkdir( $fileDir , 0777);
$j++;
}
$runTime = getMsecTime() - $startTime;

echo 'Running time:' , $runTime , '<br>';
echo 'Memory usage:' . memory_get_usage();


测试结果

php5.2.10

Running time:0.056291103363037

Memory usage:83392

php5.3.6

Running time:0.057452917098999

Memory usage:628000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: