5

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...?

SilentGhost
  • 307,395
  • 66
  • 306
  • 293

4 Answers4

8

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.

  • The fastest array initialization is **`array.array('B', [0]) * 10000`**. (i.e. mupliplied short array) see http://stackoverflow.com/a/3214343/448474 – hynekcer Dec 06 '12 at 00:41
6

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).

Brian
  • 116,865
  • 28
  • 107
  • 112
  • 'B' must be used (for unsigned char) not 'b', because the former is the only one that C guarantees is a type that has all bits contributing to it's value. – Garen Aug 31 '12 at 22:39
0

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.

uzi
  • 5,085
  • 2
  • 16
  • 11
0

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.

Community
  • 1
  • 1
Josip
  • 6,677
  • 9
  • 26
  • 27