Python: Understanding Iterators And `join()` Better
The join() function accepts an iterable as parameter. However, I was wondering why having: text = 'asdfqwer' This: ''.join([c for c in text]) Is significantly faster than: ''.joi
Solution 1:
The join
method reads its input twice; once to determine how much memory to allocate for the resulting string object, then again to perform the actual join. Passing a list is faster than passing a generator object that it needs to make a copy of so that it can iterate over it twice.
A list comprehension is not simply a generator object wrapped in a list, so constructing the list externally is faster than having join
create it from a generator object. Generator objects are optimized for memory efficiency, not speed.
Of course, a string is already an iterable object, so you could just write ''.join(text)
. (Also again this is not as fast as creating the list explicitly from the string.)
Post a Comment for "Python: Understanding Iterators And `join()` Better"