python disk i/o speed

nnes pruebauno at latinmail.com
Wed Aug 7 22:40:01 CEST 2002


> 
> I susoect this last point is the most valuable data in the post, and I look
> forward to seeing the sample code.
> 
In the end I figure It would have been better to do a giant post,
anyway here comes the gory details:

###############gener1.py: generates the sample data

import sys,random
if len(sys.argv)<2:
    sys.exit("bench output")

output=file(sys.argv[1],"w")
rgen=random.Random(1)

for line in range(1,333000):
    part0=str(rgen.randrange(0,9900))
    part1=str(rgen.randrange(0,9900))
    part2=str(rgen.randrange(0,9900))
    output.write(
      "\""+part0+"\",\""+part1+"\",\""+part2+"\"\n")


##############bench1.py: python implementation

import sys,xreadlines
if len(sys.argv)<3:
    sys.exit("bench input output")

input=file(sys.argv[1])
output=file(sys.argv[2],"w")

for line in input.xreadlines():
    part=line.split("\",\"")
    part0=part[0][1:]
    part1=part[1]
    part2=part[2][:-2]
    output.write(
      "\""+part0+"\",\""+part1+"\",\""+part2+"\",\""+
        str(int(part0)+int(part1)+int(part2))+"\"\n")


##############bench1.java: java implementation

import java.io.*;
import java.util.*;

public class bench1
{

  public static void main (String[] args)throws IOException
  {
     int nparams=args.length;
     if(nparams<2)
     {
       System.out.println("bench1 inputfile outputfile");
       System.exit(1);
      }

     FileReader filein = new FileReader(args[0]);
     FileWriter fileout = new FileWriter(args[1]);

     BufferedReader r = new BufferedReader(filein);
     BufferedWriter w = new BufferedWriter(fileout);

     PrintWriter out = new PrintWriter(w);

     String sep1="\",\"";
     String sep2="\"";
     String s = r.readLine();

     while(s != null)
     {
       int p1=s.indexOf(sep1,0);
       int p2=s.indexOf(sep1,p1+1);
       int p3=s.length();
       if (p1>0 && p2>0)
       {
          String s1=s.substring(1,p1);
          String s2=s.substring(p1+3,p2);
          String s3=s.substring(p2+3,p3-1);

out.println(sep2+s1+sep1+s2+sep1+s3+sep1+
String.valueOf(Integer.parseInt(s1)+Integer.parseInt(s2)+Integer.parseInt(s3))+sep2);
       }
       s = r.readLine();

    }
out.close();
r.close();


  }

}



################bench1.c: implementation in ANSI C

#include <stdio.h>
#include<string.h>

main(int argc, char **argv)
{
	FILE	*input, *output;
	char	buff[1024],tmp[16][1024],outp[1024],sget[1024];
	char	sep[16];
        int 	length,loop,index,beg,hmuch,count;
        char    *posi,*opos[16],*auxp;
        int     sum;

        if(argc	!= 3)
	{
		printf("Usage :	fil2 inp out\n");
		exit(1);
	}

	if( (input = fopen(argv[1],"rb")) == NULL)
	{
		printf("error opening %s\n",argv[1]);

		exit(1);
	}

	if( (output = fopen(argv[2],"wb")) == NULL)
	{
		printf("error opening %s\n",argv[2]);

		exit(1);
	}

        strcpy(sep,"\",\"");

	while(1)
	{
		if(fgets(buff,1024,input)==NULL) break;
                
		length = strlen(buff);

		if(buff[length - 1] != '\n') continue;

            count=0;

	   for(count=0,posi=strstr(buff,sep);posi!=NULL;posi=strstr((posi+1),sep))
           {

                     index = buff - posi;
 
                     if(index<0) index = -index;
                     if(count==0) beg=1;

                     buff[index]='\0';
                     opos[count]=buff+beg;
                     beg=index+3;
                     count++;
           }

           index=length;
           buff[index-3]='\0';
           opos[count]=buff+beg;
           outp[0]='\"';
           outp[1]='\0';
           sum=0;

          for(loop=0;loop<=count;loop++)
          {
           strcat(outp,opos[loop]);
           sum+=atoi(opos[loop]);
           strcat(outp,"\",\"");
          }
        
          itoa(sum,sget,10);
          strcat(outp,sget);
          strcat(outp,"\"\r\n");
	  fputs(outp,output);

	}
}



More information about the Python-list mailing list