News for hackers. Stuff that matters.
Re:Python idioms by mixmastamyk on Sunday June 12, 2011 @07:13PM (#2647621)
I read many years ago that '%s%s' % (a,b) was faster than a + b, the reason given that it was done in C. But after reading this thread and trying myself, it seems to be false also. On Py 2.6:

  python -m timeit " '%s%s%s' % ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbb', 'ccccccccccccccccccccccccccccccc') "
  1000000 loops, best of 3: 0.201 usec per loop

  python -m timeit " 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 'bbbbbbbbbbbbbbbbbbbbbbbb' + 'ccccccccccccccccccccccccccccccc' "
  10000000 loops, best of 3: 0.136 usec per loop
Perhaps it changed in new versions of Python, but either way, I guess I'll be using the % form less than I used to.
Re:Python idioms by wahnfrieden on Monday June 13, 2011 @07:50PM (#2651221)
Use whatever is clearest to read, since the benchmarks vary upon circumstance, VM, and VM version.