Python is faster than C
Armin Rigo
arigo at tunes.org
Sat Apr 3 17:17:53 EST 2004
Michel Claveau/Hamster wrote:
> For the 1st April, it's finish.
Check it for yourself. Find yourself an Intel machine and grab Psyco
from http://psyco.sf.net. Here is the source of my test:
# Python Quicksort Written by Magnus Lie Hetland
# http://www.hetland.org/python/quicksort.html
def _partition(list, start, end):
pivot = list[end]
bottom = start-1
top = end
done = 0
while not done:
while not done:
bottom = bottom+1
if bottom == top:
done = 1
break
if pivot < list[bottom]:
list[top] = list[bottom]
break
while not done:
top = top-1
if top == bottom:
done = 1
break
if list[top] < pivot:
list[bottom] = list[top]
break
list[top] = pivot
return top
def _quicksort(list, start, end):
if start < end:
split = _partition(list, start, end)
_quicksort(list, start, split-1)
_quicksort(list, split+1, end)
def quicksort(list):
if len(list) > 1:
_quicksort(list, 0, len(list)-1)
# ____________________________________________________________
import random, time, psyco
l = range(100000)
random.shuffle(l)
#TIMEIT = "l.sort()"
#TIMEIT = "quicksort(l)"
TIMEIT = "psyco.proxy(quicksort)(l)"
print TIMEIT, ':',
t = time.time()
exec TIMEIT
print time.time() - t
assert l == range(100000)
More information about the Python-list
mailing list