Friday, April 29, 2011

Binary array in python

How to create big array in python, how efficient creating that

in C/C++:

byte *data = (byte*)memalloc(10000);

or

byte *data = new byte[10000];

in python...?

From stackoverflow
  • Typically with python, you'd just create a list

    mylist = []
    

    and use it as an array. Alternatively, I think you might be looking for the array module. See http://docs.python.org/library/array.html.

  • Have a look at the array module:

    import array
    array.array('B', [0] * 10000)
    

    Instead of passing a list to initialize it, you can pass a generator, which is more memory efficient.

  • You can pre-allocate a list with:

    l = [0] * 10000
    

    which will be slightly faster than .appending to it (as it avoids intermediate reallocations). However, this will generally allocate space for a list of pointers to integer objects, which will be larger than an array of bytes in C.

    If you need memory efficiency, you could use an array object. ie:

    import array, itertools
    a = array.array('b', itertools.repeat(0, 10000))
    

    Note that these may be slightly slower to use in practice, as there is an unboxing process when accessing elements (they must first be converted to a python int object).

  • You can efficiently create big array with array module, but using it won't be as fast as C. If you intend to do some math, you'd be better off with numpy.array

    Check this question for comparison.

  • l = [0] * 10000

    it's simply, fast and enough for me, thanks

0 comments:

Post a Comment