Thank you for the nice information!
For those folks who are not familiar with the psyco module, psyco allows Python to compile to native i386 code rather than the standard virtual byte code. This way the Python interpreter works much faster.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Thanks a lot kubrick, sorry I am so slow. Checked it out and you are right. I corrected the blunder.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
from heapq import heappush, heappop
import random
import time
def print_timing(func):
def wrapper(*arg):
t1 = time.clock()
res = func(*arg)
t2 = time.clock()
print '%s took %0.3fms' % (func.func_name, (t2-t1)*1000.0)
return res
return wrapper
h = []
@print_timing
def heapsort(N):
for i in range(0, N):
heappush(h, random.randint(0, N - 1))
return [heappop(h) for i in range(len(h))]
print heapsort(1000)
heapsort took 10.000ms
-ordi-
Junior Poster in Training
92 posts since Dec 2009
Reputation Points: 18
Solved Threads: 11
You producing random numbers inside tight loop. You should produce data ready in main code not to mix up timing.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
from heapq import heappush, heappop
import random
import time
def print_timing(func):
def wrapper(*arg):
t1 = time.clock()
res = func(*arg)
t2 = time.clock()
print '%s took %0.3fms' % (func.func_name, (t2-t1)*1000.0)
return res
return wrapper
h = []
@print_timing
def heapsort(N):
for i in range(0, N):
heappush(h, random.randint(0, N - 1))
return [heappop(h) for i in range(len(h))]
print heapsort(1000)
heapsort took 10.000ms
from heapq import heappush, heappop
import random
import time
def print_timing(func):
def wrapper(*arg):
t1 = time.clock()
res = func(*arg)
t2 = time.clock()
print '%s took %0.3fms' % (func.func_name, (t2-t1)*1000.0)
return res
return wrapper
h = []
N = 1000
for i in range(0, N):
heappush(h, random.randint(0, N - 1))
@print_timing
def heapsort():
k = len(h)
return [heappop(h) for i in range(k)]
heapsort()
-ordi-
Junior Poster in Training
92 posts since Dec 2009
Reputation Points: 18
Solved Threads: 11
I like one-liners but jureslak one-liner for third pseudo is not good for readability and maintainance
:)
I agree, also one-liners can be very slow.
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213