As Nicky noted, you are comparing a very fast C procedure with Python. Using psyco speeds it up for me, but you can really speed it up using a bit vector module written in C. I used bitarray and then the bit sorting method is superior to the built-in sorting for an array size of around 250,000 using psyco.
Here is the function I used:
def vec_sort2(input_li): bv = bitarray(len(input_li)) bv.setall(0) for i in input_li: bv[i] = 1 return [i for i in xrange(len(bv)) if bv[i]]
Notice also that I used list comprehension to build a sorted list that helps a bit. Using psyco and above functions with your functions, I get the following results:
test_data size is: 1000000 sort function takes 1.29699993134 vec_sort function takes 3.5150001049 vec_sort2 function takes 0.953999996185
As a side note, BitVector is not particularly optimized even for Python. Before I found bitarray, I made several different settings in the module and using my module with settings, the time for vec_sort is reduced by a second for such an array size. I did not make my changes to it, because bitarray is just a lot faster.
source share