Fastest way to generate delimited string from 1d numpy array

前端 未结 7 2219
春和景丽
春和景丽 2020-12-15 16:53

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 mathema

7条回答
  •  悲哀的现实
    2020-12-15 17:13

    Convert the numpy array into a list first. The map operation seems to run faster on a list than on a numpy array.

    e.g.

    import numpy as np
    x = np.random.randn(100000).tolist()
    for i in range(100):
        ",".join(map(str, x))
    

    In timing tests I found a consistent 15% speedup for this example

    I'll leave others to explain why this might be faster as I have no idea!

提交回复
热议问题