In article <[EMAIL PROTECTED]>, Mark Dickinson <[EMAIL PROTECTED]> wrote: >On Sep 2, 9:45 am, [EMAIL PROTECTED] wrote: >> [snip code] >> >> Thanks for that. I realise that improving the algorithm will speed >> things up. I wanted to know why my less than perfect algorithm was so >> much slower in python than exactly the same algorithm in C. Even when >> turning off gcc's optimiser with the -O0 flag, the C version is still >> >> > 100 times quicker. > >Well, for one thing, you're creating half a million xrange objects in >the course of the search. All the C code has >to do is increment a few integers. > >Mark >
Right: Mr. Dickinson's original question is entirely legitimate, and it's not adequate to respond, as some follow-ups did, with ways to improve the Python-coded algorithm. The correct answer, which I want to reinforce, is that the exhibited Python and C versions are NOT "exactly the same algorithm", at least not without more quali- fication. Part of Python expertise is to recognize that creation of xrange objects, mentioned above, is far from free. Also, -O3 gives C the opportunity, also remarked in a follow-up, to factor calculations outside their loops. -- http://mail.python.org/mailman/listinfo/python-list