Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Walter =?utf-8?q?D=C3=B6rwald?=" Newsgroups: de.comp.lang.python Subject: Re: [Python-de] strings zusammensetzen. Date: Wed, 30 Aug 2017 11:53:55 +0200 Lines: 54 Message-ID: References: <40c6ccec-4cbc-9614-dbdd-9e88cd1dccf0@gmx.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de Y/ppSMewmoDcbi2paaBdwgh7LM+/C9FJq/CzY8jP6rJQ== Return-Path: X-Original-To: python-de@python.org Delivered-To: python-de@mail.python.org X-Virus-Scanned: Debian amavisd-new at rhein.livinglogic.de In-Reply-To: X-Mailer: MailMate (1.9.6r5347) X-BeenThere: python-de@python.org X-Mailman-Version: 2.1.24 Precedence: list List-Id: Die Deutsche Python Mailingliste List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <40c6ccec-4cbc-9614-dbdd-9e88cd1dccf0@gmx.de> Xref: csiph.com de.comp.lang.python:4884 On 30 Aug 2017, at 7:26, Hermann Riemann wrote: > Am 29.08.2017 um 17:21 schrieb Walter Dörwald: > >> Python 3.6.2 (default, Jul 26 2017, 16:42:24) >> Type 'copyright', 'credits' or 'license' for more information >> IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help. >> In [1] ▶ base, revision, suffix = "foo", "bar", "baz" >> In [2] ▶ %timeit base + revision + suffix >> 208 ns ± 0.532 ns per loop (mean ± std. dev. of 7 runs, 1000000 >> loops each) >> In [3] ▶ %timeit '%s%s%s' % (base, revision, suffix) >> 315 ns ± 1.55 ns per loop (mean ± std. dev. of 7 runs, 1000000 >> loops each) >> In [4] ▶ %timeit '{}{}{}'.format(base, revision, suffix) >> 462 ns ± 1.23 ns per loop (mean ± std. dev. of 7 runs, 1000000 >> loops each) >> In [5] ▶ %timeit f'{base}{revision}{suffix}' >> 14.6 ns ± 0.00911 ns per loop (mean ± std. dev. of 7 runs, >> 100000000 loops each) > > Daraus lese ich: > a+b+c ist schneller als "%s%s%s"%(a,b,c), > "%a%b%c"%(a,b,c) ist schneller als "{}{}{}".format(a,b,c). > > Das mit 14.6 ns ist Hinweis, > das in diesem Fall keine Zwischenwerte der Art (a+b)+c erstellt > wurden. In [1] ▸ import dis In [2] ▸ base, revision, suffix = "foo", "bar", "baz" In [3] ▸ def f(): ······ ▸ return f'{base}{revision}{suffix}' ······ ▸ In [4] ▸ dis.dis(f) 2 0 LOAD_GLOBAL 0 (base) 2 FORMAT_VALUE 0 4 LOAD_GLOBAL 1 (revision) 6 FORMAT_VALUE 0 8 LOAD_GLOBAL 2 (suffix) 10 FORMAT_VALUE 0 12 BUILD_STRING 3 14 RETURN_VALUE BUILD_STRING ruft _PyUnicode_JoinArray() auf. Und _PyUnicode_JoinArray() berechnet einmal den Speicherverbrauch und kopiert dann alle Teile in den Zielspeicherbereich. > Hermann > der jetzt wieder eher os.system("rm "+dateiname) > statt os.system("rm {}".format(dateiname)) schreiben wird. Servus, Walter