file seek is slow

Metalone jcb at iteris.com
Tue Mar 9 18:56:47 EST 2010


I ran a comparison that timed 1e6 file seeks.
The tests were run with Python 2.6.4 and Microsoft Visual C 6.0 on
Windows XP with an Intel 3GHz single processor with hyperthreading.

Results:
C        : 0.812 seconds
Python: 1.458 seconds.
difference = 0.646 seconds.

If the file.seek is removed the Python loop takes 2ms so the loop
overhead is minimal.
Without pysco the loop overhead is 4.6ms and Python takes 1.866.

Any ideas what is causing the slow down over the 'C' version.

In general I have been trying to take a video application written in C+
+ and make it work in Python.
There seem to be delays in the handoff to Windows System code that
make the Python version just a touch slower in some areas, but these
slowdowns are critically effecting the work.  File seek is not a deal
breaker here, it is just the latest thing I have noticed and the
simplest to demonstrate.


Python version:
import time
def main():
    # write temp file
    SIZE = 1000
    f1 = file('video.txt', 'wb')
    f1.write('+' * SIZE)
    f1.close()

    f1 = file('video.txt', 'rb')
    t0 = time.clock()
    for i in xrange(1000000):
       f1.seek(0)
    delta = time.clock() - t0
    print "%.3f" % delta
    f1.close()

if __name__ == '__main__':
    import psyco
    psyco.full()
    main()

// 'C' version
#include <stdio.h>
#include <time.h>
#define SIZE 1000

static void main(int argc, char *argv[])
{
    FILE *f1;
    int i;
    int t0;
    float delta;
    char buffer[SIZE];

    // write temp file
    memset(buffer, (int)'+', SIZE);
    f1 = fopen("video.txt", "wb");
    fwrite(buffer, SIZE, 1, f1);
    fclose(f1);

    f1 = fopen("video.txt", "rb");
    t0 = clock();
    for (i=0; i < 1000000; i++)
    {
        fseek(f1, 0, SEEK_SET);
    }
    delta = (float)(clock() - t0) / CLOCKS_PER_SEC;
    printf("%.3f\n", delta);
    fclose(f1);
}



More information about the Python-list mailing list