PHP Speed Test's
Не то чтобы я обожаю всякие олимпиады.. Но сейчас начал писать паука, и пришлось озаботиться быстродействием. Так как язык для меня сравнительно новый, а интернетам я не доверяю - набросал скрипт, который выявляет что быстрее (и главное - насколько):
Speed test #1. ereg_replace vs preg_replace vs str_replace
Speed test #2. file_get_contents vs Curl
Победила как водится нифига не дружба..
Результаты для лентяев:
Speed test #1. ereg_replace vs preg_replace vs str_replace
ereg_replace: 0.01737904548645
preg_replace: 0.2618260383606
str_replace: 0.021202087402344
str_replace #2: 0.05107307434082
file_get_contents vs Curl
file_get_contents: 0.089239835739136
curl: 0.015820026397705
Для лоботрясов - исходник:
<?php
<html>
<head>
<title>PHP Speed Test's</title>
</head>
<body>
<?php
//echo phpinfo();
echo '<h1>'.'Speed test #1. ereg_replace vs preg_replace vs str_replace'.'</h1>';
$start=microtime(true);
for ($i=0;$i<10000;$i++){
$string=ereg_replace('foo|bar','',$string);
}
$end=microtime(true)-$start;
echo 'ereg_replace:<h3>'.$end.'</h3>';
$start=microtime(true);
for ($i=0;$i<10000;$i++){
$string=preg_replace('foo|bar','',$string);
}
$end=microtime(true)-$start;
echo 'preg_replace:<h3>'.$end.'</h3>';
$start=microtime(true);
$string='I am a foo string. bar';
$kill = array('foo','bar');
for ($i=0;$i<10000;$i++){
$string = str_replace($kill, '', $string);
}
$end=microtime(true)-$start;
echo 'str_replace:<h3>'.$end.'</h3>';
$start=microtime(true);
for ($i=0;$i<10000;$i++){
$string='Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?';
$kill = array('pleasure','pain');
$string = str_replace($kill, '', $string);
}
$end=microtime(true)-$start;
echo 'str_replace #2:<h3>'.$end.'</h3>';
echo '<h1>'.'file_get_contents vs Curl'.'</h1>';
$file = '
';
//$ip = gethostbyname('ya.ru');
echo $ip;
$start=microtime(true);
for ($i=0;$i<10;$i++){
$fh = file_get_contents&#40;$file&#41;;
$c<h3>'.$end.'</h3>';
$start=microtime(true);
for ($i=0;$i<10;$i++){
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $file);
$body=curl_exec&#40;$ch&#41;;
curl_close($ch);
}
$end=microtime(true)-$start;
echo 'curl:<h3>'.$end.'</h3>;
?>
</body>
</html>
?>