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 13:58:21 GMT Organization: Norwich University Lines: 37 Message-ID: <9mgfrtFt0U5@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 26PzzwcrJrU3eb7Am0wkpw68NpVH7Uyd6cT4lXxzjCXME380Wr Cancel-Lock: sha1:rRQoWaoeulB5EPwO69WRqweA5Ec= User-Agent: slrn/0.9.9p1/mm/ao (Win32) Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18406 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 > $ ./python -m timeit -n 1000000 '"{}".format(7.928137192)' > 1000000 loops, best of 3: 1.01 usec per loop > > In the real-world telco benchmark for _decimal, replacing the > single line > > outfil.write("%s\n" % t) > > with > > outfil.write("{}\n".format(t)) > > adds 23% to the runtime. I think %-style formatting should not > be deprecated at all. When it becomes necessary, it's possible to optimize it by hoisting out the name lookups. ... outfil_write = outfil.write append_newline = "{}\n".format ... outfil_write(append_newline(t)) ... -- Neil Cerutti