Python(2.5) reads an input file FASTER than pure C(Mingw)

SL ni at hao.com
Sat Apr 26 20:22:34 CEST 2008


"n00m" <n00m at narod.ru> schreef in bericht 
news:6a3f8226-04c6-4ee3-b5a6-f76aca44aa31 at a23g2000hsc.googlegroups.com...
> import time
> t=time.time()
> f=open('D:\\some.txt','r')
> z=f.readlines()
> f.close()
> print len(z)
> print time.time()-t
> m=input()
> print z[m]
>
>
> #include <cstdio>
> #include <cstdlib>
> #include <iostream>
> #include <ctime>
>
> using namespace std;
> char vs[1002000][99];
> FILE *fp=fopen("D:\\some.txt","r");
>
> int main() {
>    int i=0;
>    while (true) {
>        if (!fgets(vs[i],999,fp)) break;
>        ++i;
>    }

first of all I would rewrite the C loop to:

int main() {
    int i=0;
    while (fgets(vs[i],999,fp))
        ++i;
}

but I think that the difference comes from what you do in the beginning of 
the C source:

char vs[1002000][99];

this reserves 99,198,000 bytes so expect a lot of cache trashing in the C 
code!

Is there an implementation of f.readlines on the internet somewhere? 
interested to see in how they implemented it. I'm pretty sure they did it 
smarter than just reserve 100meg of data :)


>    fclose(fp);
>    cout << i << endl;
>    cout << clock()/CLOCKS_PER_SEC << endl;
>
>    int m;
>    cin >> m;
>    cout << vs[m];
>    system("pause");
> return 0;
> } 




More information about the Python-list mailing list