file seek is slow

Stefan Behnel stefan_ml at behnel.de
Fri Mar 12 03:56:00 EST 2010


Metalone, 11.03.2010 23:57:
> I just tried the seek test with Cython.
> Cython fseek() : 1.059 seconds.  30% slower than 'C'
> Python f.seek  : 1.458 secondds. 80% slower than 'C'.
>
> It is amazing to me that Cython generates a 'C' file that is 1478
> lines.

Well, it generated an optimised Python interface for your module and made 
it compilable in CPython 2.3 through 3.2. It doesn't look like your C 
module features that. ;)


> #Cython code
>
> import time
>
> cdef int SEEK_SET = 0
>
> cdef extern from "stdio.h":
>      void* fopen(char* filename, char* mode)
>      int fseek(void*, long, int)

Cython ships with a stdio.pxd that you can cimport. It looks like it 
doesn't currently define fseek(), but it defines at least fopen() and FILE. 
Patches are always welcome.


> def main():
>      cdef void* f1 = fopen('video.txt', 'rb')
>      cdef int i=1000000
>      t0 = time.clock()
>      while i>  0:
>         fseek(f1, 0, SEEK_SET)
>         i -= 1
>      delta = time.clock() - t0

Note that the call to time.clock() takes some time, too, so it's not 
surprising that this is slower than hand-written C code. Did you test how 
it scales?

Also, did you look at the generated C code or the annotated Cython code 
(cython -a)? Did you make sure both were compiled with the same CFLAGS?

Also, any reason you're not using a for-in-xrange loop? It shouldn't make a 
difference in speed, it's just more common. You even used a for loop in 
your C code.

Finally, I'm not sure why you think that these 30% matter at all. In your 
original post, you even state that seek-time isn't the "deal breaker", so 
maybe you should concentrate on the real issues?

Stefan




More information about the Python-list mailing list