7

I was wondering about it:

> %timeit sum([int(digit) for digit in str(n)])
100000 loops, best of 3: 1.52 us per loop

> %timeit sum(int(digit) for digit in str(n))
100000 loops, best of 3: 2.04 us per loop
Deneb
  • 981
  • 2
  • 9
  • 25
  • 6
    What's `n`? Surely it cannot be very big. The overhead of calling the generator repeatedly will exceed the efficiency gain of not constructing the full list. Try a bigger list. – nneonneo Jul 28 '13 at 16:14
  • @nneonneo, with n='1234567890'*100 list wins, generator lose. How much bigger n must be? – eri Jul 28 '13 at 22:26
  • transfer of one big object is faster then transporting many of small. – eri Jul 28 '13 at 22:28

0 Answers0