回答編集履歴

3

bsdfanさんのロジックを取り入れて修正。私の環境ではnumba版と比べてcython版は数割速くなるようです。

2019/05/25 17:36

投稿

hayataka2049
hayataka2049

スコア30933

test CHANGED
@@ -46,7 +46,19 @@
46
46
 
47
47
 
48
48
 
49
+ start = t.time()
50
+
51
+ result3 = cy.test_20190524_changed2(a, b, c, x, y)
52
+
53
+ print('time : ' + str(round((t.time() - start),5)) + ' [sec]')
54
+
55
+
56
+
57
+
58
+
49
- print((result1 == result2).all())
59
+ print((result1 == result2).all() and
60
+
61
+ (result2 == result3).all())
50
62
 
51
63
 
52
64
 
@@ -56,9 +68,11 @@
56
68
 
57
69
  """ =>
58
70
 
59
- time : 0.0973 [sec]
71
+ time : 0.09572 [sec]
72
+
60
-
73
+ time : 0.01765 [sec]
74
+
61
- time : 0.01946 [sec]
75
+ time : 0.01645 [sec]
62
76
 
63
77
  True
64
78
 
@@ -66,6 +80,8 @@
66
80
 
67
81
 
68
82
 
83
+
84
+
69
85
  ```
70
86
 
71
87
 
@@ -134,6 +150,56 @@
134
150
 
135
151
  @cython.wraparound(False)
136
152
 
153
+ cpdef np.ndarray[np_float_t, ndim=2] test_20190524_changed2(
154
+
155
+ np.ndarray[np_float_t, ndim=3] a,
156
+
157
+ np.ndarray[np_float_t, ndim=2] b,
158
+
159
+ np.ndarray[np_int_t, ndim=1] c,
160
+
161
+ np.ndarray[np_int_t, ndim=1] x,
162
+
163
+ np.ndarray[np_int_t, ndim=1] y):
164
+
165
+
166
+
167
+ cdef np.ndarray[np_float_t, ndim=3] d
168
+
169
+ cdef np.ndarray[np_float_t, ndim=2] img
170
+
171
+ cdef int i, j, k, num
172
+
173
+
174
+
175
+ num = 10000
176
+
177
+
178
+
179
+ d = a * b[c, 0]
180
+
181
+
182
+
183
+ img = np.zeros((10000, 10000))
184
+
185
+
186
+
187
+ for i in range(num):
188
+
189
+ for j in range(2):
190
+
191
+ for k in range(2):
192
+
193
+ img[x[i] + j , y[i] + k] += d[j, k, i]
194
+
195
+ return img
196
+
197
+
198
+
199
+ @cython.boundscheck(False)
200
+
201
+ @cython.wraparound(False)
202
+
137
203
  cpdef np.ndarray[np_int_t, ndim=1] test_20190524_origin(
138
204
 
139
205
  np.ndarray[np_float_t, ndim=3] a,

2

改行の編集

2019/05/25 17:36

投稿

hayataka2049
hayataka2049

スコア30933

test CHANGED
@@ -128,6 +128,8 @@
128
128
 
129
129
  return img
130
130
 
131
+
132
+
131
133
  @cython.boundscheck(False)
132
134
 
133
135
  @cython.wraparound(False)

1

デコレータの修正。速度はほとんど変化なし

2019/05/24 19:11

投稿

hayataka2049
hayataka2049

スコア30933

test CHANGED
@@ -92,8 +92,6 @@
92
92
 
93
93
  @cython.wraparound(False)
94
94
 
95
-
96
-
97
95
  cpdef np.ndarray[np_float_t, ndim=2] test_20190524_changed(
98
96
 
99
97
  np.ndarray[np_float_t, ndim=3] a,
@@ -130,7 +128,9 @@
130
128
 
131
129
  return img
132
130
 
131
+ @cython.boundscheck(False)
133
132
 
133
+ @cython.wraparound(False)
134
134
 
135
135
  cpdef np.ndarray[np_int_t, ndim=1] test_20190524_origin(
136
136