php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53482 Mysqli Object use is much slower than Mysqli procedural use
Submitted: 2010-12-06 11:34 UTC Modified: 2010-12-10 09:25 UTC
From: iradu at unix-world dot org Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.3.3 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: iradu at unix-world dot org
New email:
PHP Version: OS:

Further comment on this bug is unnecessary.

 

 [2010-12-06 11:34 UTC] iradu at unix-world dot org
Description:
------------
mysqli_query()
is MUCH MUCH FASTER than
mysqli::query()

also MySQL PDO sucks as PDO is for dumb developers.



Test script:
---------------
==== OBJECT STYLE:

<?php
echo 'object:<br>';
for($y=0;$y<10;++$y){
$mtime='';$starttime='';$isql=array();$mysqli='';$i=1;$query='';$array=array();$result='';$row=array();
$mtime = explode(' ',microtime());$mtime = (float)$mtime[1] + (float)$mtime[0];$starttime = $mtime;
include 'isett.iphp';
$mysqli=new mysqli($isql['h'],$isql['u'],$isql['p'],$isql['d']);
if(mysqli_connect_errno()) die('Connection Error '.mysqli_connect_errno().' : '.mysqli_connect_error());
$mysqli->set_charset('utf8');

$mysqli->query("CREATE TABLE IF NOT EXISTS persad_dsa (`id` SMALLINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,`a_name` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`a_desc` VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,PRIMARY KEY(`id`))ENGINE=MYISAM");

for($i=1;$i<2000;++$i){
  $mysqli->query('INSERT INTO persad_dsa (a_name,a_desc) VALUES(\'=+=+=+=+=+'.($i+1000).'+=+=+=+=+=\',\'------------------------------------------------'.($i+321).'-------\')');
}

$query="SELECT * FROM persad_dsa";
if($result=$mysqli->query($query)){
  while($row=$result->fetch_assoc()){
	$array[]='|-'.$row['id'].'|'.$row['a_name'].'|'.$row['a_desc'].'-|';
  }
  $result->close();
}
$mysqli->query("DROP TABLE IF EXISTS persad_dsa");
$mysqli->close();
$mtime = explode(' ',microtime());$mtime = (float)$mtime[1] + (float)$mtime[0];echo $mtime-$starttime.'<br>';
$mtime='';$starttime='';$isql=array();$mysqli='';$i=1;$query='';$array=array();$result='';$row=array();
}
echo '---<br>procedural:<br>';
for($y=0;$y<10;++$y){
$mtime='';$starttime='';$isql=array();$mysqli='';$i=1;$query='';$array=array();$result='';$row=array();
$mtime = explode(' ',microtime());$mtime = (float)$mtime[1] + (float)$mtime[0];$starttime = $mtime;
include 'isett.iphp';
$mysqli=mysqli_connect($isql['h'],$isql['u'],$isql['p'],$isql['d']);
if(mysqli_connect_errno()) die('Connection Error '.mysqli_connect_errno().' : '.mysqli_connect_error());
mysqli_set_charset($mysqli,'utf8');

mysqli_query($mysqli,"CREATE TABLE IF NOT EXISTS persad_dsa (`id` SMALLINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,`a_name` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`a_desc` VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,PRIMARY KEY(`id`))ENGINE=MYISAM");

for($i=1;$i<2000;++$i){
  mysqli_query($mysqli,'INSERT INTO persad_dsa (a_name,a_desc) VALUES(\'=+=+=+=+=+'.($i+1000).'+=+=+=+=+=\',\'------------------------------------------------'.($i+321).'-------\')');
}

$query="SELECT * FROM persad_dsa";
if($result=mysqli_query($mysqli,$query)){
  while($row=mysqli_fetch_assoc($result)){
	$array[]='|-'.$row['id'].'|'.$row['a_name'].'|'.$row['a_desc'].'-|';
  }
  mysqli_free_result($result);
}
mysqli_query($mysqli,"DROP TABLE IF EXISTS persad_dsa");
mysqli_close($mysqli);
$mtime = explode(' ',microtime());$mtime = (float)$mtime[1] + (float)$mtime[0];echo $mtime-$starttime.'<br>';
$mtime='';$starttime='';$isql=array();$mysqli='';$i=1;$query='';$array=array();$result='';$row=array();
}

?>

//==== PROCEDURAL STYLE

<?php
echo 'procedural:<br>';
for($y=0;$y<10;++$y){
$mtime='';$starttime='';$isql=array();$mysqli='';$i=1;$query='';$array=array();$result='';$row=array();
$mtime = explode(' ',microtime());$mtime = (float)$mtime[1] + (float)$mtime[0];$starttime = $mtime;
include 'isett.iphp';
$mysqli=mysqli_connect($isql['h'],$isql['u'],$isql['p'],$isql['d']);
if(mysqli_connect_errno()) die('Connection Error '.mysqli_connect_errno().' : '.mysqli_connect_error());
mysqli_set_charset($mysqli,'utf8');

mysqli_query($mysqli,"CREATE TABLE IF NOT EXISTS persad_dsa (`id` SMALLINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,`a_name` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`a_desc` VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,PRIMARY KEY(`id`))ENGINE=MYISAM");

for($i=1;$i<2000;++$i){
  mysqli_query($mysqli,'INSERT INTO persad_dsa (a_name,a_desc) VALUES(\'=+=+=+=+=+'.($i+1000).'+=+=+=+=+=\',\'------------------------------------------------'.($i+321).'-------\')');
}

$query="SELECT * FROM persad_dsa";
if($result=mysqli_query($mysqli,$query)){
  while($row=mysqli_fetch_assoc($result)){
	$array[]='|-'.$row['id'].'|'.$row['a_name'].'|'.$row['a_desc'].'-|';
  }
  mysqli_free_result($result);
}
mysqli_query($mysqli,"DROP TABLE IF EXISTS persad_dsa");
mysqli_close($mysqli);
$mtime = explode(' ',microtime());$mtime = (float)$mtime[1] + (float)$mtime[0];echo $mtime-$starttime.'<br>';
$mtime='';$starttime='';$isql=array();$mysqli='';$i=1;$query='';$array=array();$result='';$row=array();
}
echo '---<br>object:<br>';
for($y=0;$y<10;++$y){
$mtime='';$starttime='';$isql=array();$mysqli='';$i=1;$query='';$array=array();$result='';$row=array();
$mtime = explode(' ',microtime());$mtime = (float)$mtime[1] + (float)$mtime[0];$starttime = $mtime;
include 'isett.iphp';
$mysqli=new mysqli($isql['h'],$isql['u'],$isql['p'],$isql['d']);
if(mysqli_connect_errno()) die('Connection Error '.mysqli_connect_errno().' : '.mysqli_connect_error());
$mysqli->set_charset('utf8');

$mysqli->query("CREATE TABLE IF NOT EXISTS persad_dsa (`id` SMALLINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,`a_name` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`a_desc` VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,PRIMARY KEY(`id`))ENGINE=MYISAM");

for($i=1;$i<2000;++$i){
  $mysqli->query('INSERT INTO persad_dsa (a_name,a_desc) VALUES(\'=+=+=+=+=+'.($i+1000).'+=+=+=+=+=\',\'------------------------------------------------'.($i+321).'-------\')');
}

$query="SELECT * FROM persad_dsa";
if($result=$mysqli->query($query)){
  while($row=$result->fetch_assoc()){
	$array[]='|-'.$row['id'].'|'.$row['a_name'].'|'.$row['a_desc'].'-|';
  }
  $result->close();
}
$mysqli->query("DROP TABLE IF EXISTS persad_dsa");
$mysqli->close();
$mtime = explode(' ',microtime());$mtime = (float)$mtime[1] + (float)$mtime[0];echo $mtime-$starttime.'<br>';
$mtime='';$starttime='';$isql=array();$mysqli='';$i=1;$query='';$array=array();$result='';$row=array();
}
?>

//====

Expected result:
----------------
PHP 5.3 is going in a wrong direction dudes !

Actual result:
--------------
I can't predict

Patches

is_this_a_patch (last revision 2010-12-06 10:38 UTC by iradu at unix-world dot org)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-06 11:40 UTC] iradu at unix-world dot org
Procedural:
test 1: 0.4619922161
test 2: 0.4763075113
test 3: 0.43635993
Object:
test 1: 0.4942417717
test 2: 0.4999971008
test 3: 0.498214469

Procedural wins 3 of 4 tests, object oriented wins 1 of 4 tests.

Post what you think about all this.
 [2010-12-10 09:25 UTC] cataphract@php.net
-Status: Open +Status: Bogus -Block user comment: N +Block user comment: Y
 [2010-12-10 09:25 UTC] cataphract@php.net
Please stick to the point (no dumbness considerations), avoid posting big scripts and please indent them properly so they're legible. Your test is also flawed because you use microtime() instead of microtime(true).

I'd say it's possible object use is *marginally* slower than procedural use, but you haven't shown even that.

If you still find procedural use is significantly faster (> 10%), you can submit a new bug report, but please follow the guidelines above.

Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 07 17:01:27 2024 UTC