[Numpy-discussion] Decimal arrays?

Robert Kern robert.kern@gmail....
Sat Aug 20 19:08:04 CDT 2011


On Sat, Aug 20, 2011 at 17:49, Chris Withers <chris@simplistix.co.uk> wrote:
> On 20/08/2011 15:38, Robert Kern wrote:
>> On Sat, Aug 20, 2011 at 17:37, Chris Withers<chris@simplistix.co.uk>  wrote:
>>> Hi All,
>>>
>>> What's the best type of array to use for decimal values?
>>> (ie: where I care about precision and want to avoid any possible
>>> rounding errors)
>>
>> dtype=object
>
> Thanks!
>
> What are the performance implications, if any, of this array type?

It will be slower than floats, obviously, because there will be
several C function calls and plenty of extra instructions for each
operation on each element. But it will be somewhat faster than looping
in Python. Note that decimal.Decimal objects are implemented in pure
Python, so you will also be paying for Python function call overhead
and other costs going through ceval.c several times over.

You may want to try the cdecimal package:

  http://pypi.python.org/pypi/cdecimal/

This will provide an extension module defining an extension type
implemented in C. You can avoid the ceval.c overhead entirely during
the array operation.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list