summaryrefslogtreecommitdiff
path: root/scipy/weave/examples/ramp.py
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2006-01-02 08:26:24 +0000
committerTravis Oliphant <oliphant@enthought.com>2006-01-02 08:26:24 +0000
commit4712a37b93832933a46376ee99339f9040ba3670 (patch)
tree8a3de8500925061b0f2368fae2d50159dbea206f /scipy/weave/examples/ramp.py
parentb5ba0003def4cfa43b29d29df8f085d09609707b (diff)
downloadnumpy-4712a37b93832933a46376ee99339f9040ba3670.tar.gz
Moved weave to scipy
Diffstat (limited to 'scipy/weave/examples/ramp.py')
-rw-r--r--scipy/weave/examples/ramp.py137
1 files changed, 0 insertions, 137 deletions
diff --git a/scipy/weave/examples/ramp.py b/scipy/weave/examples/ramp.py
deleted file mode 100644
index 7b56c5d89..000000000
--- a/scipy/weave/examples/ramp.py
+++ /dev/null
@@ -1,137 +0,0 @@
-""" Comparison of several different ways of calculating a "ramp"
- function.
-
- C:\home\ej\wrk\junk\scipy\weave\examples>python ramp.py
- python (seconds*ratio): 128.149998188
- arr[500]: 0.0500050005001
- compiled numeric1 (seconds, speed up): 1.42199993134 90.1195530071
- arr[500]: 0.0500050005001
- compiled numeric2 (seconds, speed up): 0.950999975204 134.752893301
- arr[500]: 0.0500050005001
- compiled list1 (seconds, speed up): 53.100001812 2.41337088164
- arr[500]: 0.0500050005001
- compiled list4 (seconds, speed up): 30.5500030518 4.19476220578
- arr[500]: 0.0500050005001
-
-"""
-
-import time
-import scipy.weave as weave
-from scipy.base import *
-
-def Ramp(result, size, start, end):
- step = (end-start)/(size-1)
- for i in xrange(size):
- result[i] = start + step*i
-
-def Ramp_numeric1(result,start,end):
- code = """
- const int size = Nresult[0];
- const double step = (end-start)/(size-1);
- double val = start;
- for (int i = 0; i < size; i++)
- *result++ = start + step*i;
- """
- weave.inline(code,['result','start','end'],compiler='gcc')
-
-def Ramp_numeric2(result,start,end):
- code = """
- const int size = Nresult[0];
- double step = (end-start)/(size-1);
- double val = start;
- for (int i = 0; i < size; i++)
- {
- result[i] = val;
- val += step;
- }
- """
- weave.inline(code,['result','start','end'],compiler='gcc')
-
-def Ramp_list1(result, start, end):
- code = """
- const int size = result.len();
- const double step = (end-start)/(size-1);
- for (int i = 0; i < size; i++)
- result[i] = start + step*i;
- """
- weave.inline(code, ["result","start", "end"], verbose=2)
-
-def Ramp_list2(result, start, end):
- code = """
- const int size = result.len();
- const double step = (end-start)/(size-1);
- for (int i = 0; i < size; i++)
- {
- PyObject* val = PyFloat_FromDouble( start + step*i );
- PySequence_SetItem(py_result,i, val);
- }
- """
- weave.inline(code, ["result", "start", "end"], verbose=2)
-
-def main():
- N_array = 10000
- N_py = 200
- N_c = 10000
-
- ratio = float(N_c) / N_py
-
- arr = [0]*N_array
- t1 = time.time()
- for i in xrange(N_py):
- Ramp(arr, N_array, 0.0, 1.0)
- t2 = time.time()
- py_time = (t2 - t1) * ratio
- print 'python (seconds*ratio):', py_time
- print 'arr[500]:', arr[500]
-
- arr1 = array([0]*N_array,Float64)
- # First call compiles function or loads from cache.
- # I'm not including this in the timing.
- Ramp_numeric1(arr1, 0.0, 1.0)
- t1 = time.time()
- for i in xrange(N_c):
- Ramp_numeric1(arr1, 0.0, 1.0)
- t2 = time.time()
- c_time = (t2 - t1)
- print 'compiled numeric1 (seconds, speed up):', c_time, py_time/ c_time
- print 'arr[500]:', arr1[500]
-
- arr2 = array([0]*N_array,Float64)
- # First call compiles function or loads from cache.
- # I'm not including this in the timing.
- Ramp_numeric2(arr2, 0.0, 1.0)
- t1 = time.time()
- for i in xrange(N_c):
- Ramp_numeric2(arr2, 0.0, 1.0)
- t2 = time.time()
- c_time = (t2 - t1)
- print 'compiled numeric2 (seconds, speed up):', c_time, py_time/ c_time
- print 'arr[500]:', arr2[500]
-
- arr3 = [0]*N_array
- # First call compiles function or loads from cache.
- # I'm not including this in the timing.
- Ramp_list1(arr3, 0.0, 1.0)
- t1 = time.time()
- for i in xrange(N_py):
- Ramp_list1(arr3, 0.0, 1.0)
- t2 = time.time()
- c_time = (t2 - t1) * ratio
- print 'compiled list1 (seconds, speed up):', c_time, py_time/ c_time
- print 'arr[500]:', arr3[500]
-
- arr4 = [0]*N_array
- # First call compiles function or loads from cache.
- # I'm not including this in the timing.
- Ramp_list2(arr4, 0.0, 1.0)
- t1 = time.time()
- for i in xrange(N_py):
- Ramp_list2(arr4, 0.0, 1.0)
- t2 = time.time()
- c_time = (t2 - t1) * ratio
- print 'compiled list4 (seconds, speed up):', c_time, py_time/ c_time
- print 'arr[500]:', arr4[500]
-
-
-if __name__ == '__main__':
- main()