What is the difference between native int type and the numpy.int types?

后端 未结 3 1257
情深已故
情深已故 2020-11-30 04:37

Can you please help understand what are the main differences (if any) between the native int type and the numpy.int32 or numpy.int64 types?

3条回答
  •  迷失自我
    2020-11-30 05:03

    I think that the biggest difference is that the numpy types are compatible with their C counterparts. For one thing, this means that numpy ints can overflow...

    >>> np.int32(2**32)
    0
    

    This is why you can create an array of integers and specify the datatype as np.int32 for example. Numpy will then allocate an array that is large enough to hold the specified number of 32 bit integers and then when you need the values, it'll convert the C-integers to np.int32 (which is very quick). The benefits of being able to convert back and forth from np.int32 and a C-int also include huge memory savings. Python objects are generally pretty big:

    >>> sys.getsizeof(1)
    24
    

    A np.int32 isn't any smaller:

    >>> sys.getsizeof(np.int32(1))
    28
    

    but remember, most of the time when we're working with numpy arrays, we're only working with the C integers which only take 4 bytes (instead of 24). We only need to work with the np.int32 when dealing with scalar values from an array.

提交回复
热议问题