Is It Possible To Speed-up Conversion Of A List Into An Array In Python?
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' >
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.
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module