[Numpy-discussion] Misc Pyrex questions
Francesc Altet
faltet at carabos.com
Tue Jan 23 13:33:23 CST 2007
El 2007-01-22 23:59:03 GMT, en/na Pierre GM va escriure:
> Other example: what's more efficient ?
> myvar = <ndarray>numpy.empty(shape,dtype)
> or
> myvar = PyArray_EMPTY(dims, NPY_TYPE)
Ok. The second one indeed, but possibly less than you realize (just a
25%, see some benchmarks below). The main point is, as always, avoiding
premature optimization. You should first port your code to Pyrex, and
then concentrate on the hot points. If some python calls are critical
for you, then, go ahead and choose the C call.
> But elsewhere in the manual is given the example of a loop using `range`, when
> one should use some explicit interval, and my understanding was that using
> python expressions was not as efficient as having more proper C expressions.
> Is this the case here ? Do I have to reimplement __getitem__ on arrays, or
> could I just keep on using the current approach ?
Special methods (like __getitem__) of Pyrex extensions performs exactly
the same than a __getitem__ made in pure C extensions. So, you don't
have be worried about that.
A benchmark that I've made (I was curious too ;), and that I'm
attaching, proves this. Here is the run on a pretty old machine:
$ python run_bench.py
******************** NumPy times *********************************
time for __len__ (numpy)--> 0.203
sum (numpy)--> 49987.2991813
time for __getitem__ (numpy)--> 0.314
******************** Pyrex times *********************************
time for __len__ (pyrex)--> 0.198
sum (pyrex)--> 49987.2991813
time for __getitem__ (pyrex)--> 0.172
********* Comparing NumPy creation times (python and C) **********
time for creating an empty array (python)--> 3.305
time for creating an empty array (C)--> 2.664
In this case, the __getitem__ of Pyrex seems to perform better than the
__getitem__ of the ndarray object written in C (almost a 2x, in fact).
However, this is probably an ilusion, as the ndarray __getitem__ will do
far more work than the Pyrex one. OTOH, the __len__ method is far more
simple, and can be taken as the demonstration that the overhead of
calling special methods in Pyrex from Python is similar to C
counterparts. Finally, the difference of overhead in using a Python or a
C call for creating an empty array is shown in the last part of the
benchmark. All in all, a 25% of difference is not that much.
Cheers,
--
Francesc Altet | Be careful about using the following code --
Carabos Coop. V. | I've only proven that it works,
www.carabos.com | I haven't tested it. -- Donald Knuth
More information about the Numpy-discussion
mailing list