Efficient Python array with 100 million zeros?

后端 未结 10 707
挽巷
挽巷 2020-12-08 19:56

What is an efficient way to initialize and access elements of a large array in Python?

I want to create an array in Python with 100 million entries, unsigned 4-byte

10条回答
  •  盖世英雄少女心
    2020-12-08 20:45

    If

    • access speed of array.array is acceptable for your application
    • compact storage is most important
    • you want to use standard modules (no NumPy dependency)
    • you are on platforms that have /dev/zero

    the following may be of interest to you. It initialises array.array about 27 times faster than array.array('L', [0]*size):

    myarray = array.array('L')
    f = open('/dev/zero', 'rb')
    myarray.fromfile(f, size)
    f.close()
    

    On How to initialise an integer array.array object with zeros in Python I'm looking for an even better way.

提交回复
热议问题