Fastest way to generate delimited string from 1d numpy array
Posted
by Abiel
on Stack Overflow
See other posts from Stack Overflow
or by Abiel
Published on 2010-04-27T13:20:43Z
Indexed on
2010/04/27
13:23 UTC
Read the original article
Hit count: 352
I have a program which needs to turn many large one-dimensional numpy arrays of floats into delimited strings. I am finding this operation quite slow relative to the mathematical operations in my program and am wondering if there is a way to speed it up. For example, consider the following loop, which takes 100,000 random numbers in a numpy array and joins each array into a comma-delimited string.
import numpy as np
x = np.random.randn(100000)
for i in range(100):
",".join(map(str, x))
This loop takes about 20 seconds to complete (total, not each cycle). In contrast, consider that 100 cycles of something like elementwise multiplication (x*x) would take than one 1/10 of a second to complete. Clearly the string join operation creates a large performance bottleneck; in my actual application it will dominate total runtime. This makes me wonder, is there a faster way than ",".join(map(str, x))? Since map() is where almost all the processing time occurs, this comes down to the question of whether there a faster to way convert a very large number of numbers to strings.
© Stack Overflow or respective owner