Reputation: 1045
I would like to be able to calculate the number of bytes occupied by my dictionary when running my code with the PyPy interpreter. I do not know ahead of time what the keys of the dictionary will be, nor do I know what the values of those keys will be. This is what I know:
string
string
, int
, double
, or None
I understand that there are no direct alternatives to sys.getsizeof()
based on the explanation provided in the failure message. The answers provided here, for the most part, link to iterative solutions that implement sys.getsizeof()
in some way or another.
Entries for the dictionary are loaded from a JSON file, so they look like some variation of this format:
{'a': 'hello', 'b': None, 'c': 20, 'd': 20.5}
Is there a way that I can calculate the amount of bytes being represented by this object without using sys.getsizeof()
? I am having a hard time googling a solution and am hoping for some insight here.
Upvotes: 0
Views: 103
Reputation: 2573
That question doesn't make sense in CPython, and even less sense in PyPy. In addition to considerations of shared state, the size can change depending on what is in the dict and whether or not PyPy can optimize it (explicit strategies for only-int-keys and others), so in general it will not be constant across calls to sys.getsizeof
. See the compatibility page of the pypy docs for more info
Upvotes: 1