@@ -50,7 +50,7 @@ We have a :class:`DataFrame` to which we want to apply a function row-wise.
50
50
{
51
51
" a" : np.random.randn(1000 ),
52
52
" b" : np.random.randn(1000 ),
53
- " N" : np.random.randint(100 , 1000 , (1000 )),
53
+ " N" : np.random.randint(100 , 1000 , (1000 ), dtype = " int64 " ),
54
54
" x" : " x" ,
55
55
}
56
56
)
@@ -84,7 +84,7 @@ using the `prun ipython magic function <https://ipython.readthedocs.io/en/stable
84
84
:okexcept:
85
85
86
86
# most time consuming 4 calls
87
- % prun - l 4 df.apply(lambda x : integrate_f(x[" a " ], x[" b " ], x[" N " ]), axis = 1 ) # noqa E999
87
+ % prun - l 4 df.apply(lambda x : integrate_f(x[' a ' ], x[' b ' ], x[' N ' ]), axis = 1 )
88
88
89
89
By far the majority of time is spend inside either ``integrate_f `` or ``f ``,
90
90
hence we'll concentrate our efforts cythonizing these two functions.
@@ -166,7 +166,7 @@ can be improved by passing an ``np.ndarray``.
166
166
.. ipython :: python
167
167
:okexcept:
168
168
169
- % prun - l 4 df.apply(lambda x : integrate_f_typed(x[" a " ], x[" b " ], x[" N " ]), axis = 1 )
169
+ % prun - l 4 df.apply(lambda x : integrate_f_typed(x[' a ' ], x[' b ' ], x[' N ' ]), axis = 1 )
170
170
171
171
.. ipython ::
172
172
@@ -207,7 +207,7 @@ calls are needed to utilize this function.
207
207
.. ipython :: python
208
208
:okexcept:
209
209
210
- % timeit apply_integrate_f(df[" a " ].to_numpy(), df[" b " ].to_numpy(), df[" N " ].to_numpy())
210
+ % timeit apply_integrate_f(df[' a ' ].to_numpy(), df[' b ' ].to_numpy(), df[' N ' ].to_numpy())
211
211
212
212
Performance has improved from the prior implementation by almost ten times.
213
213
@@ -222,7 +222,7 @@ and ``wraparound`` checks can yield more performance.
222
222
.. ipython :: python
223
223
:okexcept:
224
224
225
- % prun - l 4 apply_integrate_f(df[" a " ].to_numpy(), df[" b " ].to_numpy(), df[" N " ].to_numpy())
225
+ % prun - l 4 apply_integrate_f(df[' a ' ].to_numpy(), df[' b ' ].to_numpy(), df[' N ' ].to_numpy())
226
226
227
227
.. ipython ::
228
228
@@ -258,7 +258,7 @@ and ``wraparound`` checks can yield more performance.
258
258
.. ipython :: python
259
259
:okexcept:
260
260
261
- % timeit apply_integrate_f_wrap(df[" a " ].to_numpy(), df[" b " ].to_numpy(), df[" N " ].to_numpy())
261
+ % timeit apply_integrate_f_wrap(df[' a ' ].to_numpy(), df[' b ' ].to_numpy(), df[' N ' ].to_numpy())
262
262
263
263
However, a loop indexer ``i `` accessing an invalid location in an array would cause a segfault because memory access isn't checked.
264
264
For more about ``boundscheck `` and ``wraparound ``, see the Cython docs on
0 commit comments