1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
""" 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
"""
from __future__ import absolute_import, print_function
import time
import scipy.weave as weave
from numpy 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,float)
# 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,float)
# 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()
|