Functions of esProc/R/Python/Perl in Structured Data Process by Comparison :Chapter 20. Comparing Sorting Performance

For each script, use built-in library function to sort data, and develop a bubble sort algorithm. Then their performances are presented by comparison.

esProc

esProc_r_perl_python_20.1

Perl

    sublib_sort{            # Use sort library function

         my @sorted=sort{

                   $b <=> $a

                   } @_;

         return @sorted;

    }

    my @row=();

    open(INFILE, ‘d:/bigsort.txt’);         

    while(<INFILE>)

    {

         push(@row,$_);     

    }

    my $t1=[gettimeofday];

    lib_sort(@row);

    my $t2=[gettimeofday];

    print “the sort function time is:”,tv_interval($t1,$t2),” seconds\n”;

 

    submySort {   #Self-coding

    my @a = @_;   

    my $n=@a;

    for my $i(0..$n) {

    for my $j($i+1..$n) {

       if ($a[$j]< $a[$i]) {  

            @a[$i,$j] = @a[$j,$i];           # Member swap

                                 }

                                 }

                             }

    return @a;

    }

    @row=();

    open(INFILE, ‘d:/sort.txt’);               

    while(<INFILE>)

    {

         push(@row,$_);     

    }

    $t1=[gettimeofday];

     mySort(@row);

    $t2=[gettimeofday];

    print “the sort time is:”,tv_interval($t1,$t2),” seconds\n”;

Python

    #!python

    import time

 

    myfile = open(“d:/bigsort.txt”)                  # Use a library function

    row= ( line for line in myfile)

         

    myfile.close()

    start=time.time()

    row.sort()

    end=time.time()

    print(“sort time:”,end-start)

 

    defmySort(A):           #Self-coding

         n= len(A)

         fori in range(0,n-1):

                   for j in range(i+1,n-1):

                            if A[i]>A[j]:

                                     (A[i],A[j])=(A[j],A[i])

 

    myfile = open(“d:/sort.txt”)             

    row= ( line for line in myfile)

   

    myfile.close()

    start=time.time()

    mySort(row)

    end=time.time()

    print(“my sort time:”,end-start)

R

     library(timeDate)

 

    data<-read.table(“d:/bigsort.txt”)            #Use a library function

    start=Sys.timeDate()      

    print(start)

    data<-data[order(data[,1],decreasing=TRUE),]     

    end=Sys.timeDate()

    print(end)

    print(end-start)

  

    data<-read.table(“d:/sort.txt”)       #Self-coding

    data1<-data[,1]                 #Convert the data frame into a list

    start=Sys.timeDate()

    print(start)      

    n=length(data1)

    for (i in 1:n){

         j=i+1

         while(j<=n){

                   if(data1[i]>data1[j]){

                            tmp=data1[j]

                            data1[j]=data1[i]

                            data1[i]=tmp

                }

                   j=j+1

         }

    }

    end=Sys.timeDate()

    print(end)

    print(end-start)

 

 

esProc_r_perl_python_20.2

Advertisements

About datathinker

a technical consultant on Database performance optimization, Database storage expansion, Off-database computation. personal blog at: datakeywrod, website: raqsoft
This entry was posted in esProc/R/Python/Perl, Structured Data Process and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s