Monday, July 7, 2014

Python: To format or to concatenate

A while ago a kindly reader pointed out that Python's string .format method is, after all, a function, and carries with it some over head. I have some inner loop code that I could stand to run a little faster and I was looking for a way to speed things up without losing readability. In one part of the loop I was creating a string using the format statement. I wondered if I could speed things up by changing that to a concat.

So I first tested it out on some toy code:

def str_format(size=int(1e6)):
  for n in range(size):
    a = 'hi {:d} {:d} {:d}'.format(n, n+1, n+2)
  return a


def str_concat(size=int(1e6)):
  for n in range(size):
    a = 'hi ' + str(n) + str(n+1) + str(n+2)
  return a

In [448]: %timeit str_concat()
1 loops, best of 3: 996 ms per loop

In [449]: %timeit str_format()
1 loops, best of 3: 1.26 s per loop

So, the plain python concat is faster than the elegant way. This held with one variable or two variables too. This probably has to do with the complexity of the .format function, which offsets even the extra calls to str







No comments:

Post a Comment