diff options
author | Travis Oliphant <oliphant@enthought.com> | 2005-09-26 20:20:16 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2005-09-26 20:20:16 +0000 |
commit | 45d01a4be1c4221132ba46d687e6af3a8df3329b (patch) | |
tree | ce3be5290e918def7c7187e747c5460193b0ca85 /weave/examples/functional.py | |
parent | ccd1c3db37672627aa4fe0fdb5437f5dddc0fe86 (diff) | |
download | numpy-45d01a4be1c4221132ba46d687e6af3a8df3329b.tar.gz |
Moved weave
Diffstat (limited to 'weave/examples/functional.py')
-rw-r--r-- | weave/examples/functional.py | 98 |
1 files changed, 0 insertions, 98 deletions
diff --git a/weave/examples/functional.py b/weave/examples/functional.py deleted file mode 100644 index 015cfc0c1..000000000 --- a/weave/examples/functional.py +++ /dev/null @@ -1,98 +0,0 @@ -# C:\home\eric\wrk\scipy\weave\examples>python functional.py -# desired: [2, 3, 4] -# actual: [2, 3, 4] -# actual2: [2, 3, 4] -# python speed: 0.039999961853 -# SCXX speed: 0.0599999427795 -# speed up: 0.666666666667 -# c speed: 0.0200001001358 -# speed up: 1.99998807913 - -import sys -sys.path.insert(0,'..') -import inline_tools -from types import * -def c_list_map(func,seq): - """ Uses CXX C code to implement a simple map-like function. - It does not provide any error checking. - """ - assert(type(func) in [FunctionType,MethodType,type(len)]) - code = """ - #line 22 "functional.py" - py::tuple args(1); - int N = seq.len(); - py::list result(N); - for(int i = 0; i < N;i++) - { - args[0] = seq[i]; - result[i] = func.call(args); - } - return_val = result; - """ - return inline_tools.inline(code,['func','seq']) - -def c_list_map2(func,seq): - """ Uses Python API more than CXX to implement a simple map-like function. - It does not provide any error checking. - """ - assert(type(func) in [FunctionType,MethodType,type(len)]) - code = """ - #line 40 "functional.py" - py::tuple args(1); - PyObject* py_args = (PyObject*)args; - py::list result(seq.len()); - PyObject* py_result = (PyObject*)result; - PyObject* item = NULL; - PyObject* this_result = NULL; - int N = seq.len(); - for(int i = 0; i < N;i++) - { - item = PyList_GET_ITEM(py_seq,i); - Py_INCREF(item); - PyTuple_SetItem(py_args,0,item); - this_result = PyEval_CallObject(py_func,py_args); - PyList_SetItem(py_result,i,this_result); - } - return_val = result; - """ - return inline_tools.inline(code,['func','seq']) - -def main(): - seq = ['aa','bbb','cccc'] - print 'desired:', map(len,seq) - print 'actual:', c_list_map(len,seq) - print 'actual2:', c_list_map2(len,seq) - -def time_it(m,n): - import time - seq = ['aadasdf'] * n - t1 = time.time() - for i in range(m): - result = map(len,seq) - t2 = time.time() - py = t2 - t1 - print 'python speed:', py - - #load cache - result = c_list_map(len,seq) - t1 = time.time() - for i in range(m): - result = c_list_map(len,seq) - t2 = time.time() - c = t2-t1 - print 'SCXX speed:', c - print 'speed up:', py / c - - #load cache - result = c_list_map2(len,seq) - t1 = time.time() - for i in range(m): - result = c_list_map2(len,seq) - t2 = time.time() - c = t2-t1 - print 'c speed:', c - print 'speed up:', py / c - -if __name__ == "__main__": - main() - time_it(100,1000)
\ No newline at end of file |