All pastes #570382 Raw Edit

Miscellany

public php v1 · immutable
·published 2007-06-16 17:40 UTC
rendered paste body
<center><div style="width: 500px; height: 200px; overflow: scroll"><?php$db='benchmark';$host='localhost';$pass='';$user='root';$results = 10000;$mysqli=mysqli_connect($host,$user,$pass,$db);$pdo=new PDO('mysql:dbname='.$db.';host='.$host,$user,$pass);$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);$mysqlo=new MySQLi($host,$user,$pass,$db);$old=mysql_connect($host,$user,$pass);mysql_select_db($db,$old);$t1=array();$t2=array();$t3=array();$t4=array();echo '';#MySQLi - procedural//for all while queries: What the hell are those?for($i=0;$i<$results;$i++){    #MySQL    $t=microtime(true);    $res=mysql_query('SELECT title,created,option1,floatnum,text FROM test',$old);    while($array=mysql_fetch_assoc($res))    {        echo $array['floatnum'].' - ';    }    $t1[$i]=microtime(true)-$t;    mysql_free_result($res);        unset($res,$array);        #MySQLi - proceduralny    $t=microtime(true);    $res=mysqli_query($mysqli,'SELECT title,created,option1,floatnum,text FROM test',MYSQLI_USE_RESULT);    while($array=mysqli_fetch_assoc($res))    {        echo $array['floatnum'].' - ';    }    $t2[$i]=microtime(true)-$t;    mysqli_free_result($res);        unset($res,$array);        #MySQLi - obiektowy    $t=microtime(true);    $res=$mysqlo->query('SELECT title,created,option1,floatnum,text FROM test',MYSQLI_USE_RESULT);    while($array=$res->fetch_assoc())    {        echo $array['floatnum'].' - ';    }    $t3[$i]=microtime(true)-$t;    $res->free();        unset($res,$array);        #PDO    $t=microtime(true);    $res=$pdo->query('SELECT title,created,option1,floatnum,text FROM test');    $res->setFetchMode(PDO::FETCH_ASSOC);    while($array = $res->fetch())    {        echo $array['floatnum'].'<br /><br />';    }    $t4[$i]=microtime(true)-$t;    $res->closeCursor();    unset($res);        unset($array);}#Zestawienie?></div><table cellspacing="9" cellpadding="3"><tr>    <th>Count</th>    <th>MySQL (proc.)</th>    <th>MySQLi (proc.)</th>    <th>MySQLi (obj)</th>    <th>PDO &rarr;</th></tr><?php$ile=$results;//count($t1);$mavg = array();$minmax = array();for($i=0,$ii=0;$i<$ile;++$i){    $min=min($t1[$i],$t2[$i],$t3[$i],$t4[$i]);    $max=max($t1[$i],$t2[$i],$t3[$i],$t4[$i]);    for($y = 1; $y < 5; $y++) {	if (!isset($mavg[$y])) $mavg[$y] = 0;	$mavg[$y] += (${"t$y"}[$i]) * 1000;    }    echo '    <tr>        <td>'.++$ii.'.</td>';        for ($x = 1; $x < 5; $x++) { 	        if($min==${"t$x"}[$i])        	{			echo '<td style="color: green">';			if (!isset($minmax['max'][$x])) $minmax['max'][$x] = 0;			$minmax['max'][$x] += 1;        	}	        elseif($max==${"t$x"}[$i])        	{			echo '<td style="color: red">';			if (!isset($minmax['min'][$x])) $minmax['min'][$x] = 0;			$minmax['min'][$x] += 1;        	}	        else        	{	            echo '<td>';        	}	        echo ${"t$x"}[$i]*1000 .' ms</td>';	}    echo '</tr>';}echo '<tr><td> Stats: </td>';for ($x = 1; $x < 5; $x++) {	echo '<td>Avg : '.  ($mavg[$x] / $results) . ' ms <br />Max : '.$minmax['max'][$x] .' <br />Min : '.$minmax['min'][$x].'<br />Mid : '.($results - $minmax['min'][$x] - $minmax['max'][$x]).'</td>';	//print_r($mmax[$x]);}//print count($mmax[1]);?></tr></table></center>