diff options
author | Eric Jones <eric@enthought.com> | 2002-01-03 19:50:46 +0000 |
---|---|---|
committer | Eric Jones <eric@enthought.com> | 2002-01-03 19:50:46 +0000 |
commit | 6e13c0408681ef82fc5f70d002d34993f19cc659 (patch) | |
tree | fbaba261253d70c119dcdbf343dbfccb84fbcad1 /weave/examples/fibonacci.py | |
parent | 7161eb8ef2587dea3e8066bf209d0fe715057d0c (diff) | |
download | numpy-6e13c0408681ef82fc5f70d002d34993f19cc659.tar.gz |
renaming compiler to weave
Diffstat (limited to 'weave/examples/fibonacci.py')
-rw-r--r-- | weave/examples/fibonacci.py | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/weave/examples/fibonacci.py b/weave/examples/fibonacci.py new file mode 100644 index 000000000..ac0116641 --- /dev/null +++ b/weave/examples/fibonacci.py @@ -0,0 +1,145 @@ +# Typical run: +# C:\home\ej\wrk\scipy\compiler\examples>python fibonacci.py +# Recursively computing the first 30 fibonacci numbers: +# speed in python: 3.98599994183 +# speed in c: 0.0800000429153 +# speed up: 49.82 +# Loopin to compute the first 30 fibonacci numbers: +# speed in python: 0.00053100001812 +# speed in c: 5.99999427795e-005 +# speed up: 8.85 +# fib(30) 832040 832040 832040 832040 + +import sys +sys.path.insert(0,'..') +import ext_tools + +def build_fibonacci(): + """ Builds an extension module with fibonacci calculators. + """ + mod = ext_tools.ext_module('fibonacci_ext') + a = 1 # this is effectively a type declaration + + # recursive fibonacci in C + fib_code = """ + int fib1(int a) + { + if(a <= 2) + return 1; + else + return fib1(a-2) + fib1(a-1); + } + """ + ext_code = """ + int val = fib1(a); + return_val = Py::new_reference_to(Py::Int(val)); + """ + fib = ext_tools.ext_function('c_fib1',ext_code,['a']) + fib.customize.add_support_code(fib_code) + mod.add_function(fib) + + # looping fibonacci in C + fib_code = """ + int fib2( int a ) + { + int last, next_to_last, result; + + if( a <= 2 ) + return 1; + last = next_to_last = 1; + for(int i = 2; i < a; i++ ) + { + result = last + next_to_last; + next_to_last = last; + last = result; + } + + return result; + } + """ + ext_code = """ + int val = fib2(a); + return_val = Py::new_reference_to(Py::Int(val)); + """ + fib = ext_tools.ext_function('c_fib2',ext_code,['a']) + fib.customize.add_support_code(fib_code) + mod.add_function(fib) + mod.compile() + +try: + import fibonacci_ext +except ImportError: + build_fibonacci() + import fibonacci_ext +c_fib1 = fibonacci_ext.c_fib1 +c_fib2 = fibonacci_ext.c_fib2 + +################################################################# +# This where it might normally end, but we've added some timings +# below. Recursive solutions are much slower, and C is 10-50x faster +# than equivalent in Python for this simple little routine +# +################################################################# + +def py_fib1(a): + if a <= 2: + return 1 + else: + return py_fib1(a-2) + py_fib1(a-1) + +def py_fib2(a): + if a <= 2: + return 1 + last = next_to_last = 1 + for i in range(2,a): + result = last + next_to_last + next_to_last = last + last = result + return result; + +import time + +def recurse_compare(n): + print 'Recursively computing the first %d fibonacci numbers:' % n + t1 = time.time() + for i in range(n): + py_fib1(i) + t2 = time.time() + py = t2- t1 + print ' speed in python:', t2 - t1 + + #load into cache + c_fib1(i) + t1 = time.time() + for i in range(n): + c_fib1(i) + t2 = time.time() + print ' speed in c:',t2 - t1 + print ' speed up: %3.2f' % (py/(t2-t1)) + +def loop_compare(m,n): + print 'Loopin to compute the first %d fibonacci numbers:' % n + t1 = time.time() + for i in range(m): + for i in range(n): + py_fib2(i) + t2 = time.time() + py = (t2-t1) + print ' speed in python:', (t2 - t1)/m + + #load into cache + c_fib2(i) + t1 = time.time() + for i in range(m): + for i in range(n): + c_fib2(i) + t2 = time.time() + print ' speed in c:',(t2 - t1)/ m + print ' speed up: %3.2f' % (py/(t2-t1)) + +if __name__ == "__main__": + n = 30 + recurse_compare(n) + m= 1000 + loop_compare(m,n) + print 'fib(30)', c_fib1(30),py_fib1(30),c_fib2(30),py_fib2(30)
\ No newline at end of file |