Surprising (for me) benchmark results...

Isaac To kkto at csis.hku.hk
Tue May 1 23:06:57 EDT 2001


>>>>> "E" == E Mark Ping <emarkp at CSUA.Berkeley.EDU> writes:

    E> The argument was that Java was faster than C++.  It turned out that
    E> it was faster, because my implementation of C++ had an available
    E> patch that I hadn't applied.  Anyway, the reults for sorting a 30K
    E> line file (about 4.3MB) were:

    E> Java: ~1500 ms C++: ~950 ms Perl: ~1000 ms

    E> And then, just for kicks I wrote the following python script:

    E> import time
    E> start = time.time()
    E> f = open("infile.txt", 'r')
    E> out = open("outfile.txt", 'w')
    E> l = f.readlines()
       ^^^^^^^^^^^^^^^^^
    E> l.sort()
    E> out.writelines(l)
    E> f.close()
    E> out.close()
    E> end = time.time()
    E> print (end-start), " seconds"

How you read and write in C++?  This is probably where the difference lies.
Last year when my students use C++ to read a file, they find that switching
from using ifstream to cstdio makes the program four times faster.  Of
course they are using the slow stdlibc++ library of the old g++, and I'm yet
to tested it on the new stdlibc++ version 3.  But I bet it won't really be a
whole lot faster, since stdlibc++ v3 basically tackle the comformance
problem rather than the performance problem.

BTW, C++ sort is faster than Python sort, because it doesn't need to first
check the type of the object.  This really comes down to language design
problems, but of course Python is not supposed to run fast.  Though, I'd
like the type checking which allow me to say "this is a string, and it will
forever be a string".  Not really because of performance, but because it
gets a lot of stupid bugs when I'm sleepy and careless.

Regards,
Isaac.



More information about the Python-list mailing list