Reputation: 3215
I have read from different sources that size of integer variable is unbounded in python and it grows with the size of the integer itself. I have few question related to a code that I am working on in this context.
- Is there a lower bound? Or does it literally starts from 1 byte and grows according to integer size?
- Can a lower bound or any bound for that matter of fact be applied to integer variables in python?
- If I have an array of integers is it likely that each index has different number of bytes depending on the integer it is holding? or Does python guarantee uniform size in arrays?
What I am trying to do is to calculate memory bandwidth by noting the time taken to sum up a really huge array and then use this time and size of array to roughly estimate the bandwidth. But in order to do so I need to know the bytes read from the memory and if they are not uniform then it is really infeasible to check each individual index of the array as the array has around 10M indexes. Any alternate suggestions?
Upvotes: 2
Views: 1174
Reputation: 119
As the comment already stated sys.getsizof(0) = 24 gives you the lower bound and the entries do not point to objects of the same size.
But in order to do so I need to know the bytes read from the memory and if they are not uniform then it is really infeasible to check each individual index of the array as the array has around 10M indexes. Any alternate suggestions?
I do not think its infeasible, in a post- or preprocessing you can easily do for your largeArray
sum([sys.getsizeof(x) for x in largeArray])
Nevertheless you should be aware, that Integers below 255 are singletons and do not use different memory.
Upvotes: 0
Reputation: 140168
Is there a lower bound? Or does it literally starts from 1 byte and grows according to integer size?
int
is an object. It certainly cannot be only 1 byte long.
The lower bound can be obtained with sys.getsizeof(0)
On my machine:
>>> sys.getsizeof(0)
24
>>> sys.getsizeof(10000000000000000000000)
36
>>> sys.getsizeof(1<<31)
32
>>> sys.getsizeof(10000000000000000000000000000000000000000000)
44
In Python 2, int
uses the native integer until it's not possible, after that it's using long
. In Python 3, everything is long
, so the value 24
is version & machine dependent (32/64 bit), but there is a lower bound.
Can a lower bound or any bound for that matter of fact be applied to integer variables in python?
Lower bound yes, as discussed above, upper bound: no. Any integer can be stored in memory as long as you have enough memory. As shown above, the bigger the integer is, the bigger the integer object is.
If you know the max value your integers can have, then yes there is an upper bound.
If I have an array of integers is it likely that each index has different number of bytes depending on the integer it is holding? or Does python guarantee uniform size in arrays?
Unlike C arrays, the array holds references of integers. So the array size itself is easily predictable.
>>> sys.getsizeof([1000000000000000000000]*10)
144
>>> sys.getsizeof([1000000000000000000000]*20)
224
>>> sys.getsizeof([1]*20)
224
>>>
See how the values stored inside didn't influence the result, but only the array size?
That's because you have to account for the size of each int
object, which, as shown above, is variable, and that's the heart of your predictive problem.
Upvotes: 1