Performance of int/long in Python 3

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Mar 25 20:49:42 EDT 2013


On 26 March 2013 00:17, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Mon, 25 Mar 2013 16:16:05 -0700, Ethan Furman wrote:
>
[snip]
>> If you're working with
>> numbers, and speed is an issue, you really should be using one of the
>> numeric or scientific packages out there.
>
[snip]
> What I would like to see though is a module where I can import fixed-
> width signed and unsigned integers that behave like in C, complete with
> overflow, for writing code that matches the same behaviour as other
> languages.

Numpy can do this:

>>> import numpy
>>> a = numpy.array([0], numpy.uint32)
>>> a
array([0], dtype=uint32)
>>> a[0] = -1
>>> a
array([4294967295], dtype=uint32)

Unfortunately it doesn't work with numpy "scalars", so to use this
without the index syntax you'd need a wrapper class. Also it uses
Python style floor rounding rather than truncation as in C (actually I
seem to remember discovering that in C this is implementation
defined).

Presumably ctypes has something like this as well.


Oscar



More information about the Python-list mailing list