PHP mysqli procedural style vs object oriented style

Discussion in 'MySQL' started by ignas2526, Sep 26, 2009.

  1. #1
    PHP 5 introduces MySQLi which is improved MySQL extension. MySQLi can be implemented in two styles: object oriented style and procedural style.
    Does anybody know which of the styles has better performance?
    Thanks.
     
    ignas2526, Sep 26, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    They're about the same, but the object method is probably slightly slower. If there is a difference it would be pretty much negligible. The object method makes it much easier to use transactions, and the code is generally much cleaner and easier to understand. Additionally, the object method gives you the benefits of objects.

    I would suggest using what you're most comfortable with. But, understanding and using the object method can help speed up your coding, and your scripts end up being easier to understand.
     
    jestep, Sep 26, 2009 IP
  3. ignas2526

    ignas2526 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well as far as I know object orientation ends up as an another layer, since pure code is still procedural style. Well about what OO is easier I doubt it, for me procedural style is easier to understand, and who cares about how understandable code is, most people nowadays care about how small, powerful and optimized it is.
    But since MySQLi is written in C and is compiled already, OO can be quicker, that's why I'm not sure here.
    If PHP's MySQLi class ends up using functions from procedural style, then procedural style is faster...
     
    ignas2526, Sep 26, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    What you can do is write some simple queries and run them 500 or 1000 times consecutively. Use microtime to benchmark and compare the 2. I would run each one 5 - 10 times and average the results.

    I have to disagree about small, optimized scripts at the expense of objects. In my experience, people want stable, understandable, and standards based programming. The overhead for an object layer in the case of a database is going to be negligible, and the benefits from it are numerous.
     
    jestep, Sep 26, 2009 IP
  5. ignas2526

    ignas2526 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I made a test to find out which is faster.
    Script1: Object Oriented then Procedural:
    <?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();
    }
    
    ?>
    Code (markup):
    Script2:procedural then Object Oriented:
    <?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();
    }
    ?>
    Code (markup):
    In both cases queries are same, and methods are same to, the only difference is in code style.
    Results:
    procedural:
    0.682552099228
    0.464617013931
    0.490931034088
    0.447603940964
    0.492651939392
    0.483375072479
    0.504446983337
    0.488202095032
    0.474863052368
    0.482882022858
    ______________
    0.5012125254
    ---
    object:
    0.439932107925
    0.438354969025
    0.428317070007
    0.442404985428
    0.442044973373
    0.450672864914
    0.425189971924
    0.442571878433
    0.427703142166
    0.427068948746
    ______________
    0.4364260912
    
    
    procedural:
    0.498822927475
    0.456892967224
    0.450288057327
    0.446386098862
    0.464179992676
    0.470333099365
    0.459476947784
    0.463217973709
    0.444230079651
    0.466094017029
    ______________
    0.4619922161
    ---
    object:
    0.451704978943
    0.450787067413
    0.442464828491
    0.451838970184
    0.500449895859
    0.503927946091
    0.567903995514
    0.498856067657
    0.509907960892
    0.464576005936
    ______________
    0.4842417717
    
    
    object:
    0.712810993195
    0.46958899498
    0.457010984421
    0.455898046494
    0.508882045746
    0.45121383667
    0.443696975708
    0.455738067627
    0.459833145142
    0.48529791832
    ______________
    0.4899971008
    ---
    procedural:
    0.473474025726
    0.494134187698
    0.459471940994
    0.473254919052
    0.470777988434
    0.50800204277
    0.50657081604
    0.477627038956
    0.454518079758
    0.445244073868
    ______________
    0.4763075113
    
    
    object:
    0.584923028946
    0.547205924988
    0.483695983887
    0.450005054474
    0.450945854187
    0.457696914673
    0.442692995071
    0.493937969208
    0.493886947632
    0.477154016495
    ______________
    0.488214469
    ---
    procedural:
    0.419256925583
    0.428467035294
    0.431293010712
    0.437885046005
    0.480365991592
    0.429268121719
    0.434899091721
    0.443602085114
    0.425256967545
    0.433305025101
    ______________
    0.43635993
    Code (markup):
    Procedural:
    test 1: 0.5012125254
    test 2: 0.4619922161
    test 3: 0.4763075113
    test 4: 0.43635993
    Object:
    test 1: 0.4364260912
    test 2: 0.4842417717
    test 3: 0.4899971008
    test 4: 0.488214469

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

    Post what you think about all this.
     
    ignas2526, Sep 27, 2009 IP