Who is best with MySQL? R or Perl or Python: A benchmark

I really surprised to see R talks better with MySQL than Perl or Python!!!... I thought R might be slow in querying MySQL database. But I have a different answer here. Following is the code used for this demo.

R script:
library(RMySQL)
test <- function(){
         con <- dbConnect("MySQL", host="genome-mysql.cse.ucsc.edu", user ="genome", dbname = "hg18")
         rs <- dbGetQuery(con, "select name, chromEnd from snp129 where chrom='chr1' and chromStart between 1 and 1e8;")
         return(rs)
         dbClearResult(rs)
         dbDisconnect(con)
        }
print(system.time(test()))
   user  system elapsed
  0.372   0.039  25.729
R took 25.729 seconds to retrieve half million records.

Python script:
from time import time
import MySQLdb
start=time()
def test():
        Conn = MySQLdb.connect(host="genome-mysql.cse.ucsc.edu",user='genome',db="hg18")
        Cursor = Conn.cursor()
        SQL="select name, chromEnd from snp129 where chrom='chr1' and chromStart between 1 and 1e8;"
        Cursor.execute(SQL)
        rSQL = Cursor.fetchall()
        Conn.close()
test()
print time() - start
Whereas, Python took 28.0789999962 seconds

Perl script:
use Benchmark;
use DBI;
my $count = 10;
timethis($count, \&withprepare);
sub withprepare{
        my $dbh = DBI->connect ("DBI:mysql:hg18:genome-mysql.cse.ucsc.edu", 'genome') or die "Couldn't connect to database: ".DBI->errstr;
        my $sth = $dbh->prepare('select name, chromEnd from snp129 where chrom="chr1" and chromStart between 1 and 1e8');
        $sth->execute();
}

Perl took 26.9 seconds.

I am more happy to say that R is not slow rather it is much faster with MySQL

Comments

Popular posts from this blog

Unzipping password protected files of Michigan Imputation Server

Multiple repos in SVN

Scatter Plot Matrix in Lattice: A way to join multiple scatter plot matrices in one plot