2 ms·
Solid points. There are a few common patterns that tend to slow Python down though. For instance lists are dynamically allocated, so repeatedly appending to th
by rectangletangle 7y ago
Solid points. There are a few common patterns that tend to slow Python down though.
For instance lists are dynamically allocated, so repeatedly appending to the list may cause it to reallocate.
lst = []
for i in range(10_000_000):
lst.append(i)
Runs in ~1.67 seconds on my system, using 3.7 (this behavior is also generally true for older versions like Python 2).
However due to the leaky abstraction, you can force allocation all at once which speeds things up, albeit at the loss of clarity.
length = 10_000_000
lst = [None] * length
for i in range(length):
lst[i] = i
Runs in ~1.21 seconds.
However in these trivial cases Python offers more idiomatic approaches, which also perform better.
lst = [i for i in range(10_000_000)]
Runs in ~0.44 seconds.
lst = list(range(10_000_000))
Runs in ~0.22 seconds.
Strings behave similarly when comparing the join method to incremental appending.
string = ''
for _ in range(10_000_000):
string += 'a'
Is slower than the more idiomatic join/comprehension.
string = ''.join('a' for _ in range(10_000_000))
Or even more concisely:
string = 'a' * 10_000_000
Still the trade-off is worth the versatility most of the time IMO. And like you pointed out you can always drop down to C if necessary.