2

To save memory, I want to use less bytes (4) for each int I have instead of 24.

I looked at structs, but I don't really understand how to use them. https://docs.python.org/3/library/struct.html

When I do the following:

myInt = struct.pack('I', anInt)

sys.getsizeof(myInt) doesn't return 4 like I expected.

Is there something that I am doing wrong? Is there another way for Python to save memory for each variable?

ADDED: I have 750,000,000 integers in an array that I wish to be able to use given an index.

8
  • why do you want to do this? Commented Jun 25, 2019 at 21:34
  • 2
    I think you are trying to solve the problem of a solution to a problem. Could you elaborate more about the real problem? Commented Jun 25, 2019 at 21:35
  • In modern computers, saving just a few bytes is far more programmer work than is helpful. This is true even for most embedded computers or small ones like the Raspberry Pi. What you ask would only help if you wanted to do it for many integers held in a larger data structure, such as an array. Is that the case for you? Which data structure are you using? (The particular structure matters for your question.) Commented Jun 25, 2019 at 21:38
  • I have 750,000,000 integers in an array that I wish to be able to use given an index. Commented Jun 25, 2019 at 21:39
  • Do you want these 750M ints in memory to search? You can read it page by page from the original data source and search on it. Commented Jun 25, 2019 at 21:40

1 Answer 1

3

If you want to hold many integers in an array, use a numpy ndarray. Numpy is a very popular third-party package that handles arrays more compactly than Python alone does. Numpy is not in the standard library so that it could be updated more frequently than Python itself is updated--it was considered to be added to the standard library. Numpy is one of the reasons Python has become so popular for Data Science and for other scientific uses.

Numpy's np.int32 type uses four bytes for an integer. Declare your array full of zeros with

import numpy as np
myarray = np.zeros((750000000,), dtype=np.int32)

Or if you just want the array and do not want to spend any time initializing the values,

myarray = np.empty((750000000,), dtype=np.int32)

You then fill and use the array as you like. There is some Python overhead for the complete array, so the array's size will be slightly larger than 4 * 750000000, but the size will be close.

Sign up to request clarification or add additional context in comments.

2 Comments

What are your thoughts on the array module that is included in the standard library?
@NoctisSkytower: As far as I can tell, the only advantage of the array module is that it is part of the standard library. It is inferior to numpy in just about every other way. I wouldn't be surprised if it remains only for compatibility reasons. As I wrote, including numpy in the standard library was seriously considered but rejected for upgrading reasons. One key fact is that other popular third-party libraries, including matplotlib and pandas, use numpy rather than array. I cannot think of any such library that uses array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.