Fastest way to generate delimited string from 1d numpy array

前端 未结 7 2186
春和景丽
春和景丽 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:39

    Very good writeup on the performance of various string concatenation techniques in Python: http://www.skymind.com/~ocrow/python_string/

    I'm a little surprised that some of the latter approaches perform as well as they do, but looks like you can certainly find something there that will work better for you than what you're doing there.

    Fastest method mentioned on the site

    Method 6: List comprehensions

    def method6():
      return ''.join([`num` for num in xrange(loop_count)])
    

    This method is the shortest. I'll spoil the surprise and tell you it's also the fastest. It's extremely compact, and also pretty understandable. Create a list of numbers using a list comprehension and then join them all together. Couldn't be simpler than that. This is really just an abbreviated version of Method 4, and it consumes pretty much the same amount of memory. It's faster though because we don't have to call the list.append() function each time round the loop.

    0 讨论(0)
提交回复
热议问题