Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > de.comp.lang.python > #4885
| From | Mike Müller <mmueller@python-academy.de> |
|---|---|
| Newsgroups | de.comp.lang.python |
| Subject | Re: [Python-de] strings zusammensetzen. |
| Date | 2017-08-30 16:14 +0200 |
| Organization | Python Academy GmbH & Co. KG |
| Message-ID | <mailman.305.1504102489.2689.python-de@python.org> (permalink) |
| References | (7 earlier) <FBBAF2F3-FD7E-4E31-B76D-96CDB4B4199E@livinglogic.de> <mailman.288.1504021504.2689.python-de@python.org> <f0n0kkFt57tU1@mid.individual.net> <ADA0227F-D493-42FF-B7DF-51E448CF6214@livinglogic.de> <1273b1e6-734a-b955-81c6-f851144f46ba@python-academy.de> |
Am 30.08.17 um 11:53 schrieb Walter Dörwald:
> 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)
Die konkrete Umsetzung ist bei Zeitmessungen immer wichtig.
Anstatt des Zugriffs auf die globalen Variablen:
%timeit '{}{}{}'.format(base, revision, suffix)
421 ns ± 4.23 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
die Anfangswerte direkt mit %%time zu setzen:
%%timeit base, revision, suffix = "foo", "bar", "baz"
'{}{}{}'.format(base, revision, suffix)
367 ns ± 5.77 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
bringt hier ähnliche Ergebnisse.
Aber für die Variante mit f-String sieht das anders aus:
%timeit f'{base}{revision}{suffix}'
14.1 ns ± 0.127 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
%%timeit base, revision, suffix = "foo", "bar", "baz"
f'{base}{revision}{suffix}'
98.6 ns ± 2.02 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
Die letzte Variante scheint mir die realistischere Variante zu sein.
In der schnelleren Variante ist wahrscheinlich Caching im Spiel.
Schlussfolgerung: f-Strings sind immer noch schneller, aber halt nicht gleich
mehr als eine Größenordnung sondern eher um den Faktor 3-4.
Mike
>>
>> 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
> _______________________________________________
> python-de maillist - python-de@python.org
> https://mail.python.org/mailman/listinfo/python-de
Back to de.comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
strings zusammensetzen. Hermann Riemann <nospam.ng@hermann-riemann.de> - 2017-08-25 07:45 +0200
Re: [Python-de] strings zusammensetzen. Mike Müller <mmueller@python-academy.de> - 2017-08-25 08:00 +0200
Re: [Python-de] strings zusammensetzen. Stefan Behnel <python-de@behnel.de> - 2017-08-25 08:05 +0200
Re: [Python-de] strings zusammensetzen. Stefan Schwarzer <sschwarzer@sschwarzer.net> - 2017-08-25 09:08 +0200
Re: [Python-de] strings zusammensetzen. "Tobias Herp" <tobias.herp@gmx.de> - 2017-08-25 10:28 +0200
Re: [Python-de] strings zusammensetzen. ole-usenet-spam@gmx.net (Оlе Ѕtrеісhеr) - 2017-08-25 10:47 +0200
Re: [Python-de] strings zusammensetzen. Tobias Herp <tobias.herp@gmx.de> - 2017-08-25 23:28 +0200
Re: [Python-de] strings zusammensetzen. ole-usenet-spam@gmx.net (Оlе Ѕtrеісhеr) - 2017-08-26 10:30 +0200
Re: [Python-de] strings zusammensetzen. Peter Otten <__peter__@web.de> - 2017-08-26 13:29 +0200
Re: [Python-de] strings zusammensetzen. "Walter Dörwald" <walter@livinglogic.de> - 2017-08-29 17:21 +0200
Re: [Python-de] strings zusammensetzen. Hermann Riemann <nospam.ng@hermann-riemann.de> - 2017-08-30 07:26 +0200
Re: [Python-de] strings zusammensetzen. Stefan Behnel <python-de@behnel.de> - 2017-08-30 07:48 +0200
Re: [Python-de] strings zusammensetzen. Hermann Riemann <nospam.ng@hermann-riemann.de> - 2017-08-30 08:04 +0200
Re: [Python-de] strings zusammensetzen. ole-usenet-spam@gmx.net (Оlе Ѕtrеісhеr) - 2017-08-30 08:23 +0200
Re: [Python-de] strings zusammensetzen. Hermann Riemann <nospam.ng@hermann-riemann.de> - 2017-08-30 09:37 +0200
Re: [Python-de] strings zusammensetzen. Peter Otten <__peter__@web.de> - 2017-08-30 10:23 +0200
Re: [Python-de] strings zusammensetzen. Hermann Riemann <nospam.ng@hermann-riemann.de> - 2017-08-30 20:00 +0200
Re: [Python-de] strings zusammensetzen. "Peter Heitzer" <peter.heitzer@rz.uni-regensburg.de> - 2017-08-30 08:30 +0000
Re: [Python-de] strings zusammensetzen. Hermann Riemann <nospam.ng@hermann-riemann.de> - 2017-08-30 20:03 +0200
Re: [Python-de] strings zusammensetzen. Thomas Orgelmacher <trash@odbs.org> - 2017-08-30 20:21 +0200
Re: [Python-de] strings zusammensetzen. Hermann Riemann <nospam.ng@hermann-riemann.de> - 2017-08-31 14:31 +0200
Re: [Python-de] strings zusammensetzen. Thomas Orgelmacher <trash@odbs.org> - 2017-08-31 19:26 +0200
Re: [Python-de] strings zusammensetzen. Peter Otten <__peter__@web.de> - 2017-08-30 21:24 +0200
Re: [Python-de] strings zusammensetzen. Hermann Riemann <nospam.ng@hermann-riemann.de> - 2017-08-31 14:40 +0200
Re: [Python-de] strings zusammensetzen. Peter Otten <__peter__@web.de> - 2017-08-31 15:26 +0200
Re: [Python-de] strings zusammensetzen. "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2017-09-16 09:45 +0200
Re: [Python-de] strings zusammensetzen. Thomas Orgelmacher <trash@odbs.org> - 2017-08-31 19:11 +0200
Re: [Python-de] strings zusammensetzen. Hermann Riemann <nospam.ng@hermann-riemann.de> - 2017-09-01 09:12 +0200
Re: [Python-de] strings zusammensetzen. Thomas Orgelmacher <trash@odbs.org> - 2017-09-01 21:06 +0200
Re: [Python-de] strings zusammensetzen. Stefan Behnel <python-de@behnel.de> - 2017-09-01 21:43 +0200
Re: [Python-de] strings zusammensetzen. Arnold Krille <arnold@arnoldarts.de> - 2017-09-02 15:23 +0200
Re: [Python-de] strings zusammensetzen. "Walter Dörwald" <walter@livinglogic.de> - 2017-08-30 11:53 +0200
Re: [Python-de] strings zusammensetzen. Mike Müller <mmueller@python-academy.de> - 2017-08-30 16:14 +0200
Re: [Python-de] strings zusammensetzen. Mike Müller <mmueller@python-academy.de> - 2017-08-25 11:18 +0200
Re: [Python-de] strings zusammensetzen. Stefan Schwarzer <sschwarzer@sschwarzer.net> - 2017-08-25 12:40 +0200
Re: [Python-de] strings zusammensetzen. Tobias Herp <tobias.herp@gmx.de> - 2017-08-25 23:41 +0200
Re: [Python-de] strings zusammensetzen. "Dr. Volker Jaenisch" <volker.jaenisch@inqbus.de> - 2017-08-26 02:34 +0200
Re: strings zusammensetzen. Thomas Orgelmacher <trash@odbs.org> - 2017-08-29 19:05 +0200
Re: strings zusammensetzen. ole-usenet-spam@gmx.net (Оlе Ѕtrеісhеr) - 2017-08-30 08:32 +0200
Re: strings zusammensetzen. "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2017-09-16 09:28 +0200
Re: [Python-de] strings zusammensetzen. Stefan Behnel <python-de@behnel.de> - 2017-09-16 10:33 +0200
Re: [Python-de] strings zusammensetzen. "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2017-09-16 22:46 +0200
Re: [Python-de] strings zusammensetzen. Stefan Behnel <python-de@behnel.de> - 2017-09-17 08:19 +0200
Re: [Python-de] strings zusammensetzen. "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2017-09-17 12:34 +0200
Re: [Python-de] strings zusammensetzen. ole-usenet-spam@gmx.net (Оlе Ѕtrеісhеr) - 2017-09-17 10:50 +0200
Re: [Python-de] strings zusammensetzen. Stefan Behnel <python-de@behnel.de> - 2017-09-17 11:14 +0200
Re: [Python-de] strings zusammensetzen. "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2017-09-17 14:19 +0200
Re: strings zusammensetzen. Hermann Riemann <nospam.ng@hermann-riemann.de> - 2017-09-16 16:19 +0200
Re: [Python-de] strings zusammensetzen. Stefan Behnel <python-de@behnel.de> - 2017-09-16 17:30 +0200
csiph-web