Ad

Is It Possible To Speed-up Conversion Of A List Into An Array In Python?

- 1 answer

In my code, I have noticed that the conversion of a list into an array takes a significant amount of time.

I'm wondering if there any faster ways on how to convert a list to an array in python, here are my three attempts:

import numpy as np
from timeit import  timeit
from array import array


added_data = range(100000)

def test1():
    np.asarray(added_data, dtype=np.float16)

def test2():
    np.array(added_data, dtype=np.float16)

def test3():
    array('f', added_data)

print(timeit(test1,number=100))
print(timeit(test2,number=100))
print(timeit(test3,number=100))

In other words:

Input: < type 'list' >

Output: < type 'array.array' >

Ad

Answer

It is very unlikely that there's a faster way to convert a list of values into an array than the obvious and simple approaches you've already tried. If there was a better way, the numpy authors probably would have implemented it in np.asarray or the np.array constructor itself. I also want to note that array.array creates a much less sophisticated object than the numpy functions, so it's probably not what you want.

What you might be able to do to improve your program's overall performance is to avoid creating the list in the first place. Perhaps you can read external data from a file directly into an array with np.loadtxt or np.load (depending on how it is formatted). Or maybe you can generate the array from scratch with functions like np.arange, rather than using a normal Python function like range that (in Python 2) returns a list.

Ad
source: stackoverflow.com
Ad