Mi scuso in anticipo sia perche' mi sto rispondendo da solo sia per la lunghezzza dalla risp infina sia per aver usato il link a questa pagina come testo sorgente del test nella funz file_get_contents(...).
Ho buttato giu' questo test :
Codice PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Documento senza titolo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
$testo =file_get_contents( "http://forum.html.it/forum/showthread.php?s=&postid=11992015#post11992015" );
function extractLinksXPL($testo)
{
$tokens = explode("href=\"" , $testo);
$out = array();
for($i=1 ; $i<count($tokens) ; $i++)
{
$tmp = substr( $tokens[$i] , 0 , strpos($tokens[$i] , "\"" ) );
array_push($out , $tmp);
}
return $out;
}
function extractLinksREG($testo)
{
$pattern = "/^(href=\"?)[[:alnum:]].[[:alnum:]]{2,4}/i";
$matches ;
preg_match_all($pattern,$testo,$matches);
//preg_match_all("/^(http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}'.'((:[0-9]{1,5})?/.*)?$/i",$testo,$matches);
##preg_match_all("/(<([\w]+)[^>]*> )(.*)(<\/\\2> )/", $html, $matches);
return $matches;
}
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
$uno = getMicrotime();
$res = extractLinksXPL($testo);
$due = getMicrotime();
echo "EXPLODE time : ".($due-$uno);
echo "
Result ".count($res)." resources found
";
echo "<div style=\"display:block; height:100px; overflow:auto\">";
/*foreach($res as $n=>$v )
echo $v."
";
*/
echo "<h1>".var_dump($res)."</h1>";
echo "</div>";
echo "<hr color=\"red\">";
$uno = getMicrotime();
$res = extractLinksREG($testo);
$due = getMicrotime();
echo "REG EXP time : ".($due-$uno);
echo "
Result ".count($res)." resources found
";
echo "<div style=\"display:block; height:100px; overflow:auto\">";
echo "<h1>".var_dump( $res )."</h1>";
echo "</div>";
echo "<hr color=\"red\">";
?>
</body>
</html>
E questi sono i risultati:
Codice PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Documento senza titolo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
EXPLODE time : 0.00181889533997
Result 258 resources found
<div style="display:block; height:100px; overflow:auto">array(258) {
[0]=>
string(34) "http://rss.html.it/rss_forum39.xml"
[1]=>
string(57) "http://utenti.html.it/common/css/forum.all.css?ver=031207"
[2]=>
string(9) "index.php"
[3]=>
string(18) "http://www.html.it"
[4]=>
string(1) "#"
[5]=>
string(19) "javascript:void(0);"
[6]=>
string(19) "javascript:void(0);"
[7]=>
....... eccetera ho troncato i link che sono davvero tanti
}
<h1></h1></div><hr color="red">REG EXP time : 0.00984120368958
Result 2 resources found
<div style="display:block; height:100px; overflow:auto">array(2) {
[0]=>
array(0) {
}
[1]=>
array(0) {
}
}
<h1></h1></div><hr color="red">
</body>
</html>
EDIT:
senza postare altro codice ho fatto qlke refresh della pagina per vedere i tempi
il primo valore e' la funzionie con explode e il secondo e' con la reg exp.
0.00224781036377
0.000127792358398
0.00183296203613
0.000123977661133
0.0021071434021
0.000216007232666
Effettivamente la reg exp e' una sceggi rispetto alla explode, ma non da alcun risultato. Forse ho sbalgiato io magari i ris ci sono da qlke parte ma nessun var_dump mi ha dato conferma di cio'. Forse il pattern e' sbagliato.
Cmq alla fine vedo che 8 semplici righe di codice scritte in 30 secondi mi danno i risultati che cercavo in 0.002 sec.
A sto punto cosa mi cambia averli in 0.002 piuttosto che NON averli in 0.0002 secondi?
C'e' davvero bisogno di tta sta velocita'?
Mah fate voi.
P.S. se ho sbaglaito qlkosa nel codice php chiedo scusa lo fatto in 5 min. Magari ripetendo il test la reg exp ritorna qlkosa invece che una serie di var vuote.
Mah. fate vobis.
La semplicita' a volte e' una bella cosa.