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
|
from __future__ import absolute_import, print_function
import time
from scipy import weave
force = 0
N = 1000000
def list_append_scxx(a,Na):
code = """
for(int i = 0; i < Na;i++)
a.append(i);
"""
weave.inline(code,['a','Na'],force=force,verbose=2,compiler='gcc')
def list_append_c(a,Na):
code = """
for(int i = 0; i < Na;i++)
{
PyObject* oth = PyInt_FromLong(i);
int res = PyList_Append(py_a,oth);
Py_DECREF(oth);
if(res == -1)
{
PyErr_Clear(); //Python sets one
throw_error(PyExc_RuntimeError, "append failed");
}
}
"""
weave.inline(code,['a','Na'],force=force,compiler='gcc')
def list_append_py(a,Na):
for i in xrange(Na):
a.append(i)
def time_list_append(Na):
""" Compare the list append method from scxx to using the Python API
directly.
"""
print('list appending times:', end=' ')
a = []
t1 = time.time()
list_append_c(a,Na)
t2 = time.time()
print('py api: ', t2 - t1, '<note: first time takes longer -- repeat below>')
a = []
t1 = time.time()
list_append_c(a,Na)
t2 = time.time()
print('py api: ', t2 - t1)
a = []
t1 = time.time()
list_append_scxx(a,Na)
t2 = time.time()
print('scxx: ', t2 - t1)
a = []
t1 = time.time()
list_append_c(a,Na)
t2 = time.time()
print('python: ', t2 - t1)
#----------------------------------------------------------------------------
#
#----------------------------------------------------------------------------
def list_copy_scxx(a,b):
code = """
for(int i = 0; i < a.length();i++)
b[i] = a[i];
"""
weave.inline(code,['a','b'],force=force,verbose=2,compiler='gcc')
def list_copy_c(a,b):
code = """
for(int i = 0; i < a.length();i++)
{
int res = PySequence_SetItem(py_b,i,PyList_GET_ITEM(py_a,i));
if(res == -1)
{
PyErr_Clear(); //Python sets one
throw_error(PyExc_RuntimeError, "append failed");
}
}
"""
weave.inline(code,['a','b'],force=force,compiler='gcc')
def time_list_copy(N):
""" Compare the list append method from scxx to using the Python API
directly.
"""
print('list copy times:', end=' ')
a = [0] * N
b = [1] * N
t1 = time.time()
list_copy_c(a,b)
t2 = time.time()
print('py api: ', t2 - t1, '<note: first time takes longer -- repeat below>')
a = [0] * N
b = [1] * N
t1 = time.time()
list_copy_c(a,b)
t2 = time.time()
print('py api: ', t2 - t1)
a = [0] * N
b = [1] * N
t1 = time.time()
list_copy_scxx(a,b)
t2 = time.time()
print('scxx: ', t2 - t1)
a = [0] * N
b = [1] * N
t1 = time.time()
list_copy_c(a,b)
t2 = time.time()
print('python: ', t2 - t1)
if __name__ == "__main__":
time_list_copy(N)
|