Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.051 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'yeah,': 0.09; 'python': 0.11; '24,': 0.16; 'different,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:format': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'bit': 0.19; '(but': 0.19; 'figures': 0.19; 'slightly': 0.19; '>>>': 0.22; 'import': 0.22; 'looks': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "d'aprano": 0.31; 'sep': 0.31; 'steven': 0.31; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'received:google.com': 0.35; 'doing': 0.36; 'method': 0.36; 'similar': 0.36; 'received:209': 0.37; 'handle': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'hardware': 0.61; 'face': 0.64; 'more': 0.64; 'effectively': 0.66; 'million': 0.74; '50%': 0.78; 'subject:skip:o 10': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=Hla8AiHK4Q2hogUZbWBnvIBgEvih5g1tk4mXo9ptKhs=; b=kjR3pWJhkvL5CefDwKZeFJ3bFTOV7LArFuWGM4EBgcHb8dH5t6s9xHQq3Dv1jJ2SBj aflLQqHdJtfHSya2VoAHNwGox+nYIPjz7nE+coo03I7InxUb70Sr2yr750VpOsMYm/Q6 cMttZDh9ISBGTZ51IsHcjTMNEdmg/najSPLCY6fG1NjG49VsCvx3YuPDms3W6057Yy0G Lt8jPumMkVHFi8X+1JJIBfraRvgaOPsp02TMkg6hm4hvErKjxdMpMBsADHQ2fP+C8ssv ZDf4dS40HZoswVAHqqeGOD7MGEnbgZI3TTijB7tlUwgLCjLwwVVK4E9G+YAP+w7sG3ri JeMQ== MIME-Version: 1.0 X-Received: by 10.220.169.132 with SMTP id z4mr7706185vcy.28.1366728778234; Tue, 23 Apr 2013 07:52:58 -0700 (PDT) In-Reply-To: <51769c74$0$29977$c3e8da3$5496439d@news.astraweb.com> References: <51760754$0$29872$c3e8da3$5496439d@news.astraweb.com> <51769c74$0$29977$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 24 Apr 2013 00:52:58 +1000 Subject: Re: percent faster than format()? (was: Re: optomizations) From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366728786 news.xs4all.nl 2185 [2001:888:2000:d::a6]:58109 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44186 On Wed, Apr 24, 2013 at 12:36 AM, Steven D'Aprano wrote: > # Using Python 3.3. > > py> from timeit import Timer > py> setup = "a = 'spam'; b = 'ham'; c = 'eggs'" > py> t1 = Timer("'%s, %s and %s for breakfast' % (a, b, c)", setup) > py> t2 = Timer("'{}, {} and {} for breakfast'.format(a, b, c)", setup) > py> print(min(t1.repeat())) > 0.8319804421626031 > py> print(min(t2.repeat())) > 1.2395259491167963 > > > Looks like the format method is about 50% slower. Figures on my hardware are (naturally) different, with a similar (but slightly more pronounced) difference: >>> sys.version '3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)]' >>> print(min(t1.repeat())) 1.4841416995735415 >>> print(min(t2.repeat())) 2.5459869899666074 >>> t3 = Timer("a+', '+b+' and '+c+' for breakfast'", setup) >>> print(min(t3.repeat())) 1.5707538248576327 >>> t4 = Timer("''.join([a, ', ', b, ' and ', c, ' for breakfast'])", setup) >>> print(min(t4.repeat())) 1.5026834416105999 So on the face of it, format() is slower than everything else by a good margin... until you note that repeat() is doing one million iterations, so those figures are effectively in microseconds. Yeah, I think I can handle a couple of microseconds. ChrisA