以PHP 随机数打点测试这篇文章的内容为例,测试其打点所耗时间.
代码如下:
//开始时间
$stime=microtime(true);
$width=500;
$height=500;
$image=imagecreatetruecolor($width, $height);
$black=imagecolorallocate($image, 0, 0, 0);
$background=imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, $width, $height, $background);
for($i=100000;$i--;){
$x=mt_rand(0,$width);
$y=mt_rand(0,$height);
imagesetpixel($image, $x, $y, $black);
}
//结束时间
$etime=microtime(true);
$total=$etime-$stime;
$str_total=var_export($total,true);
if(substr_count($str_total,'E')){
$float_total=floatval(substr($str_total,5));
$total=$float_total/100000;
}
//将时间输出到图像中
imagefilledrectangle($image, 0, 0, $width, 14, $background);
imagestring($image,14,0,0,$total,$black);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
输出到浏览器的结果: