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

Carl Banks pavlovevidence at gmail.com
Sat Apr 26 11:43:19 EDT 2008


On Apr 26, 11:10 am, n00m <n... at narod.ru> wrote:
> Both codes below read the same huge(~35MB) text file.
> In the file > 1000000 lines, the length of each line < 99 chars.
>
> Stable result:
> Python runs ~0.65s
> C : ~0.70s
>
> Any thoughts?

Yes.

Most of the dirty work in the Python example is spent in tight loop
written in C.  This is very likely to be faster on Python on Windows
than your "C" example for several reasons:

1. Python is compiled with Microsoft's C compiler, which produces more
optimal code than Mingw.

2. The Python readline() function has been in the library for a long
time and has had time for many developers to optimize it's
performance.

3. Your "pure C" code isn't even C, let alone pure C.  It's C++.  On
most systems, the C++ iostream libraries have a lot more overhead than
C's stdio.

And, finally, we must not fail to observe that you measured these
times without startup, which is obviousy much greater for Python.  (Of
course, we only need to point this so it's not misunderstood that
you're claiming this Python process will terminate faster than the C++
one.)


So, I must regrettably opine that your example isn't very meaningful.


> 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;
>     }
>     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