Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

starting to add perf in python

  • Loading branch information...
commit 76072cdaef876f600830986cd671742d82ce012d 1 parent 1015fac
@JeffBezanson JeffBezanson authored
Showing with 61 additions and 0 deletions.
  1. +61 −0 test/perf.py
View
61 test/perf.py
@@ -0,0 +1,61 @@
+def fib(n):
@alanjds
alanjds added a note

Python's fibonacci is using bare integers, but Python have not built-in JIT. Have you tried to use psyco, the the-facto standard for JIT in Python. I dont think that it will beat, but at least will be a fair comparison.

@pao Collaborator
pao added a note

I'd argue PyPy would be the better target--it's better than psyco and also actively developed. I'm interested in seeing the PyPy runs of these benchmarks, but IIRC some still depend on NumPy arrays, which aren't implemented for PyPy yet.

@dcjones Collaborator
dcjones added a note

PyPy has implemented parts of numpy in the numpypy module. The mandel, rand_mat_stat, and rand_mat_mul benchmarks don't run because numpy.random and numpy.linalg aren't implement, but the rest run without any changes.

@Dani…|~ » python perf.py                                                                                                                                                                                                            15:37:35
python,fib,3.40795516968
python,parse_int,2.74395942688
python,mandel,5.12599945068
python,quicksort,20.4730033875
python,pi_sum,829.207897186
python,rand_mat_stat,184.453010559
python,rand_mat_mul,99.2691516876
@Dani…|~ » pypy perf.pypy                                                                                                                                                                                                            15:37:48
python,fib,5.04088401794
python,parse_int,0.226020812988
python,quicksort,2.66313552856
python,pi_sum,34.0478420258
@Dani…|~ » 
@pao Collaborator
pao added a note

Nice. I knew it was work in progress, and I've been checking the PyPy status blog occasionally, but hadn't actually tried to run the benchmark yet. Amazing the difference it makes, it's really excellent work by that team.

@alanjds
alanjds added a note

"fib" results looks strange for me...

I could argue that, in such recursive cases, one will usually decorate fib with functools.lru_cache which is included into standard lib (in fact, fib is its example of use), but it seems unfair with other languages that does not have it. Still thinking about why its taking so much time, even more on pypy

@dcjones Collaborator
dcjones added a note

Want to see something even stranger? If you change the code to

def fib(n):
    if n == 0: return 0
    if n == 1: return 1
    return fib(n - 1) + fib(n - 2)

then it runs 0.2 seconds on pypy and 4.9 seconds on python. It seems returning n somehow defeats the pypy jit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if n<2:
+ return n
+ return fib(n-1)+fib(n-2)
+
+def partition(list, start, end):
+ pivot = list[end] # Partition around the last value
+ bottom = start-1 # Start outside the area to be partitioned
+ top = end # Ditto
+
+ done = 0
+ while not done: # Until all elements are partitioned...
+
+ while not done: # Until we find an out of place element...
+ bottom = bottom+1 # ... move the bottom up.
+
+ if bottom == top: # If we hit the top...
+ done = 1 # ... we are done.
+ break
+
+ if list[bottom] > pivot: # Is the bottom out of place?
+ list[top] = list[bottom] # Then put it at the top...
+ break # ... and start searching from the top.
+
+ while not done: # Until we find an out of place element...
+ top = top-1 # ... move the top down.
+
+ if top == bottom: # If we hit the bottom...
+ done = 1 # ... we are done.
+ break
+
+ if list[top] < pivot: # Is the top out of place?
+ list[bottom] = list[top] # Then put it at the bottom...
+ break # ...and start searching from the bottom.
+
+ list[top] = pivot # Put the pivot in its place.
+ return top # Return the split point
+
+
+def quicksort(list, start, end):
+ if start < end: # If there are two or more elements...
+ split = partition(list, start, end) # ... partition the sublist...
+ quicksort(list, start, split-1) # ... and sort both halves.
+ quicksort(list, split+1, end)
+ else:
+ return
+
+
+if __name__=="__main__": # If this script is run as a program:
+ import sys
+ import time
+ import random
+
+ t1=time.time()
+ f = fib(20)
+ print "fib: ", time.time()-t1
+
+ lst = [ random.random() for i in range(1,5000) ]
+ t1=time.time()
+ quicksort(lst, 0, len(lst)-1)
+ print "sort: ", time.time()-t1
Please sign in to comment.
Something went wrong with that request. Please try again.