a php developer weblog

blog Closed!
calin view of the web development world

2005/3/12

small db benchmark: perl, php cli and php cgi

Tags:
@ 08:35 AM (38 months, 21 days ago)

I recently run some command line scripts, and I had the option to choose between PHP-CLI and Perl. The Command Line PHP version proved to be a powerful alternative to Perl. Php is version 4.3.10, the cli version, and Perl with version 5.8.3, both running on a powerful FreeBSD machine. The scripts I used are here:

pear_db.php

#!/usr/local/bin/php
<?php

require_once 'DB.php';
user = 'your_username'
;
$pass = 'your_pass'
;
$host = 'your_server'
;
$db_name = 'your_database'
;

$dsn = "mysql://$user:$pass@$host/$db_name"
;
$db = DB::connect($dsn);
if (
DB::isError($db)) {die ($db->getMessage
());}

$query = "SELECT * FROM option_blog"
;
$result = $db->query($query
);
while (
$row = $result->fetchRow
()) {
    
$id = $row[0
];
    print
" - $idn"
;
}

$db->disconnect
();
?>

and dbi.pl:

#!/usr/bin/perl -w  
use strict; 
use DBI; 
use Data::Dumper;  
 
# database information 
my $connectionInfo="DBI:mysql:database=your_database;your_server:your_port";  
 
# make connection to database 
my $dbh = DBI->connect($connectionInfo,'your_username','your_password');  
 
# prepare and execute query 
my $query = "SELECT * FROM option_blog"; 
my $sth = $dbh->prepare($query); 
$sth->execute();   
 
my $row;  
 
# output employee list 
print "Employees in the inventory database:n"; 
 
while($row = $sth->fetchrow()) {
        print " - $rown";
 }   
$sth->finish();  
 
# disconnect from database 
$dbh->disconnect;  

Running a basic benchmark on the script reveals:

% time ./pear_db.php

0.095u 0.023s 0:00.12 91.6%     3921+2522k 0+0io 0pf+0w

% time ./dbi.pl

0.126u 0.016s 0:00.14 92.8%     1030+1303k 0+0io 0pf+0w

The conclusion is that php-cli is a powerful alternative for shell scripting, and that a similar perl script runs with comparable speed. Both scripts were using a db abstraction layer, with Pear::DB for php-cli and DBI for Perl. PHP in its CGI version offered much worse results, 5-6 times slower than Perl, especially lagging on the db connection time. PHP-cli however is a powerful port of php to shell programming.