Efficient Python array with 100 million zeros?

后端 未结 10 718
挽巷
挽巷 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:40

    For fast creation, use the array module.

    Using the array module is ~5 times faster for creation, but about twice as slow for accessing elements compared to a normal list:

    # Create array
    python -m timeit -s "from array import array" "a = array('I', '\x00'
     * 100000000)"
    10 loops, best of 3: 204 msec per loop
    
    # Access array
    python -m timeit -s "from array import array; a = array('I', '\x00'
    * 100000000)" "a[4975563]"
    10000000 loops, best of 3: 0.0902 usec per loop
    
    # Create list
    python -m timeit "a = [0] * 100000000"
    10 loops, best of 3: 949 msec per loop
    
    # Access list
    python -m timeit  -s "a = [0] * 100000000" "a[4975563]"
    10000000 loops, best of 3: 0.0417 usec per loop
    

提交回复
热议问题