7

I have tested four situations.

1.

testList = list()
for i in range(1000000):
    testList.append(i)

2.

testList = list()
for i in range(1000000): testList.append(i)

3.

testList = list()
newAppend = testList.append
for i in range(1000000): newAppend(i)

4.

[i for i in range(1000000)]

Each time was

1 : 0.09166

2 : 0.08299

3 : 0.05003

4 : 0.04594

Why others are faster than 1?

Can I find related keywords?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
신길선
  • 79
  • 2
  • For 4 vs. 1: https://stackoverflow.com/questions/22108488/are-list-comprehensions-and-functional-functions-faster-than-for-loops – perigon Jul 26 '17 at 07:00
  • 1
    BTW, did you test many iterations using ```timeit```, or just once each? If just once, the differences probably don't mean anything. – perigon Jul 26 '17 at 07:01
  • 1
    @MartijnPieters: The duplicates do not explain why #2 is faster (which is the one highlighted in the question title), although I suspect the answer is that it's not really reliably faster. – BrenBarn Jul 26 '17 at 07:05
  • 1
    @BrenBarn: binding an attribute to a local means you don't have to look it up again in the loop. It's a simple and common optimisation for tight loops. – Martijn Pieters Jul 26 '17 at 07:10
  • @MartijnPieters: Look again at #2 vs #1. They are the same code, with #2 just having the loop body on the same line as the `for`. What you said explains #3, not #2. – BrenBarn Jul 26 '17 at 07:12
  • @BrenBarn We can add [Python | Why is accessing instance attribute is slower than local?](//stackoverflow.com/q/28597014) to the duplicate targets perhaps – Martijn Pieters Jul 26 '17 at 07:12
  • 1
    #1 and #2 are the same code. Performance is not absolute, you'll never get the exact same measurement twice. If the OP didn't use `timeit` to measure this, then the variability will be greater still. The difference between 1 and 2 in timings is due to that variability, not due to the code. – Martijn Pieters Jul 26 '17 at 07:14
  • Note that without sharing the code you used to obtain your timings, the timings are not worth much at all. – Martijn Pieters Jul 26 '17 at 07:15
  • @MartijnPieters: That's what I said too, but I think that's worth stating because it's not addressed in the duplicates. Perhaps these comments are sufficient, though. – BrenBarn Jul 26 '17 at 07:16
  • @user55449: you can discuss the duplicates with me here, no need to flag. Moderators generally will not look at technical issues, preferring to leave it to the community experts. – Martijn Pieters Jul 26 '17 at 07:17
  • @user55449: I've addressed the one vs two lines below, there is *no difference*, as the exact same bytecode is produced for either. – Martijn Pieters Jul 26 '17 at 07:18
  • 1
    @BrenBarn: We don't need to address every single issue in the question with a duplicate. If the OP wants evidence, they should look at the `dis` disassembly for both loops, and provide their timing code if they want to make a stronger case there are actual differences. – Martijn Pieters Jul 26 '17 at 07:21
  • This is because calling .append() on a list causes the list object to grow (in chunks) to make space for new elements individually, while the list comprehension gathers all elements first before creating the list to fit the elements in one go: – Thorin May 19 '18 at 10:22

0 Answers0