Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Neil Cerutti Newsgroups: comp.lang.python Subject: Re: .format vs. % Date: 3 Jan 2012 19:46:40 GMT Organization: Norwich University Lines: 30 Message-ID: <9mh490FbvuU2@mid.individual.net> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <4EFF559E.1050408@gmail.com> <4f027b79$0$29880$c3e8da3$5496439d@news.astraweb.com> <4F02DE94.8000506@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: individual.net Zmmm6g4fGI5nnz+lDCJG1w4BeFb2x3D6Obztz5TN1kl4nLXN2T Cancel-Lock: sha1:lDGiHyrCy6LQB/u+JZXs65WEmpM= User-Agent: slrn/0.9.9p1/mm/ao (Win32) Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18441 On 2012-01-03, Stefan Krah wrote: > Andrew Berg wrote: >> To add my opinion on it, I find format() much more readable and easier >> to understand (with the exception of the {} {} {} {} syntax), and would >> love to see %-style formatting phased out. > > For me the %-style is much more readable. Also, it is significantly > faster: > > $ ./python -m timeit -n 1000000 '"%s" % 7.928137192' > 1000000 loops, best of 3: 0.0164 usec per loop I have done a little more investigating, and the above is arguably not fair. Python is interpolating that string at compile time, as far as I can tell. Pretty cool, and not possible with .format, but it's just regurgitating a string literal over and over, which isn't of much interest. % is faster, but not by an order of magnitude. On my machine: C:\WINDOWS>python -m timeit -n 1000000 -s "n=7.92" "'%s' % n" 1000000 loops, best of 3: 0.965 usec per loop C:\WINDOWS>python -m timeit -n 1000000 -s "n=7.92" "'{}'.format(n)" 1000000 loops, best of 3: 1.17 usec per loop -- Neil Cerutti