6

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
322k67 gold badges312 silver badges294 bronze badges
asked Apr 15, 2009 at 10:21

4 Answers 4

10

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.

answered Apr 15, 2009 at 10:29
Sign up to request clarification or add additional context in comments.

1 Comment

The fastest array initialization is array.array('B', [0]) * 10000. (i.e. mupliplied short array) see stackoverflow.com/a/3214343/448474
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).

answered Apr 15, 2009 at 10:31

1 Comment

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

answered Apr 15, 2009 at 10:37

Comments

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.

answered Apr 15, 2009 at 10:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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