Performance comparison of Php+mysql prepare and normal query
The instance code is as follows:
<?php class Timer {public $StartTime = 0;
Public $StopTime = 0;
Public $TimeSpent = 0;
function Start () {$this->starttime = Microtime ();
function Stop () {$this->stoptime = Microtime ();
function spent () {if ($this->timespent) {return $this->timespent;
else {//http://www.manongjc.com $StartMicro = substr ($this->starttime,0,10);
$StartSecond = substr ($this->starttime,11,10);
$StopMicro = substr ($this->stoptime,0,10);
$StopSecond = substr ($this->stoptime,11,10);
$start = Floatval ($StartMicro) + $StartSecond;
$stop = Floatval ($StopMicro) + $StopSecond;
$this->timespent = $stop-$start; Return round ($this->timespent,8). '
Seconds ';
}} $timer = new timer;
$timer->start (); $mysql = new Mysqli (' LocalHost ', ' root ', ' root ', ' ganbaobao_ucenter ';
/* $query = $mysql->query ("Select Username,email from Uc_members where UID < 100000");
$result = Array (); http://www.manongjc.com/article/1194.html while ($result = $query->fetch_array ()) {$result [] = Array (' name ' => $r
esult[' username ', ' email ' => $result [' email ']);
} */$query _prepare = $mysql->prepare ("Select Username,email from Uc_members where UID <");
$id = 100000;
$query _prepare->bind_param ("i", $id);
$query _prepare->execute ();
$query _prepare->bind_result ($username, $email);
$result = Array ();
while ($query _prepare->fetch ()) {$result [] = Array (' name ' => $username, ' email ' => $email);
} $timer->stop ();
Echo ' </br> pre-query MySQL run 100,000 data time: '. $timer->spent ();
Unset ($timer);
Var_dump ($result);
Run Result:
Normal MySQL run 1000 data time: 0.011621 seconds
Normal MySQL Run 10,000 data time: 0.07766891 seconds
Normal MySQL run 100,000 data time: 0.10834217 seconds
Pre-query MySQL run 1000 data time: 0.00963211 seconds
Pre-query MySQL run 10,000 data time: 0.04614592 seconds
Pre-query MySQL run 100,000 data time: 0.05989885 seconds
Thank you for reading, I hope to help you, thank you for your support for this site!