[Python-de] Anwendung von `exec`
Christopher Arndt
chris at chrisarndt.de
So Dez 9 15:22:45 CET 2012
On 08.12.2012 15:18, Stefan Schwarzer wrote:
> On 2012-12-08 11:35, Mike Müller wrote:
>> Generell würde ich `exec` nur in sehr seltenen Fällen nehmen. Es gibt fast
>> immer eine andere, oft bessere, Lösung.
>
> das dachte ich auch - und denke ich tendenziell immer noch.
> Ich war aber überrascht, als ich neulich im
> `collections`-Modul sah, dass für `namedtuple` eine nicht
> ganz kleine Klasse mit `exec` erzeugt wird
Die Verwendung von exec für "namedtuple" ist eine
Geschwindigkeits-Optimierung, und die sind ja oft nicht hübsch. Es
gibt dazu auch ein (rejected) Ticket im Python-Roundup mit einer kurzen
Diskussion.
http://bugs.python.org/issue3974
Ich habe das mal nachgemessen und mit der Implementierung ohne "exec"
verglichen. Die Definierung der neuen namedtuple-Klasse ist dadurch zwar
schneller aber die Instanzierung deutlich langsamer.
Chris
-------------- nächster Teil --------------
Ein Dateianhang mit Binärdaten wurde abgetrennt...
Dateiname : namedtuple.py
Dateityp : text/x-python
Dateigröße : 7129 bytes
Beschreibung: nicht verfügbar
URL : <http://mail.python.org/pipermail/python-de/attachments/20121209/6d5163ba/attachment-0002.py>
-------------- nächster Teil --------------
#################################################################
# Python 2.6
Tested statement:
Point = namedtuple('Point', ('x', 'y'))
for i in range(100):
point = Point(randint(1,100), randint(1,100))
Setup:
from random import randint
from collections import namedtuple
Time for 1000 calls: [4.4250049591064453, 4.5125350952148438, 4.6623580455780029]
---------------------------------------------------------------------------
Tested statement:
for i in range(100):
point = Point(randint(1,100), randint(1,100))
Setup:
from random import randint
from collections import namedtuple
Point = namedtuple('Point', ('x', 'y'))
Time for 1000 calls: [2.7006340026855469, 2.8779668807983398, 3.643204927444458]
===========================================================================
Tested statement:
Point = namedtuple('Point', ('x', 'y'))
for i in range(100):
point = Point(randint(1,100), randint(1,100))
Setup:
from random import randint
from namedtuple import namedtuple
Time for 1000 calls: [4.3285768032073975, 4.720067024230957, 5.2262241840362549]
---------------------------------------------------------------------------
Tested statement:
for i in range(100):
point = Point(randint(1,100), randint(1,100))
Setup:
from random import randint
from namedtuple import namedtuple
Point = namedtuple('Point', ('x', 'y'))
Time for 1000 calls: [4.2973759174346924, 5.0572628974914551, 5.2733349800109863]
===========================================================================
#################################################################
# Python 2.7
Tested statement:
Point = namedtuple('Point', ('x', 'y'))
for i in range(100):
point = Point(randint(1,100), randint(1,100))
Setup:
from random import randint
from collections import namedtuple
Time for 1000 calls: [4.280940055847168, 4.78491997718811, 4.9136810302734375]
---------------------------------------------------------------------------
Tested statement:
for i in range(100):
point = Point(randint(1,100), randint(1,100))
Setup:
from random import randint
from collections import namedtuple
Point = namedtuple('Point', ('x', 'y'))
Time for 1000 calls: [2.124490976333618, 2.137373924255371, 2.380051851272583]
===========================================================================
Tested statement:
Point = namedtuple('Point', ('x', 'y'))
for i in range(100):
point = Point(randint(1,100), randint(1,100))
Setup:
from random import randint
from namedtuple import namedtuple
Time for 1000 calls: [3.182651996612549, 3.3208320140838623, 3.416329860687256]
---------------------------------------------------------------------------
Tested statement:
for i in range(100):
point = Point(randint(1,100), randint(1,100))
Setup:
from random import randint
from namedtuple import namedtuple
Point = namedtuple('Point', ('x', 'y'))
Time for 1000 calls: [2.671555995941162, 2.7031848430633545, 2.785274028778076]
===========================================================================
#################################################################
# Python 3.2
Tested statement:
Point = namedtuple('Point', ('x', 'y'))
for i in range(100):
point = Point(randint(1,100), randint(1,100))
Setup:
from random import randint
from collections import namedtuple
Time for 1000 calls: [5.386542081832886, 5.676412105560303, 6.608989000320435]
---------------------------------------------------------------------------
Tested statement:
for i in range(100):
point = Point(randint(1,100), randint(1,100))
Setup:
from random import randint
from collections import namedtuple
Point = namedtuple('Point', ('x', 'y'))
Time for 1000 calls: [2.5761518478393555, 2.582357883453369, 2.5961551666259766]
===========================================================================
Tested statement:
Point = namedtuple('Point', ('x', 'y'))
for i in range(100):
point = Point(randint(1,100), randint(1,100))
Setup:
from random import randint
from namedtuple import namedtuple
Time for 1000 calls: [3.505733013153076, 3.515310049057007, 3.523669958114624]
---------------------------------------------------------------------------
Tested statement:
for i in range(100):
point = Point(randint(1,100), randint(1,100))
Setup:
from random import randint
from namedtuple import namedtuple
Point = namedtuple('Point', ('x', 'y'))
Time for 1000 calls: [3.290466070175171, 3.306889057159424, 3.3199567794799805]
===========================================================================
-------------- nächster Teil --------------
Ein Dateianhang mit Binärdaten wurde abgetrennt...
Dateiname : time_namedtuple.py
Dateityp : text/x-python
Dateigröße : 1486 bytes
Beschreibung: nicht verfügbar
URL : <http://mail.python.org/pipermail/python-de/attachments/20121209/6d5163ba/attachment-0003.py>
-------------- nächster Teil --------------
Ein Dateianhang mit Binärdaten wurde abgetrennt...
Dateiname : signature.asc
Dateityp : application/pgp-signature
Dateigröße : 996 bytes
Beschreibung: OpenPGP digital signature
URL : <http://mail.python.org/pipermail/python-de/attachments/20121209/6d5163ba/attachment-0001.pgp>
Mehr Informationen über die Mailingliste python-de