Hoffentlich liest lunar jetzt nicht mit...
cofi hat geschrieben:Aus dem Performance-Blickwinkel ist sie auch suboptimal...
Das scheint mir manchmal schon ein Reflex zu sein: Dies oder das ist mit Listen gelöst, das muss lahm sein...
Aus meiner geringen Erfahrung heraus weiß ich aber, dass das
so oft nicht stimmt. Und nachdem mich das Tutorial schließlich 'n Hunni gekostet hat, hab' ich da gleich mal eine decorator-Übung gemacht *grins*:
Code: Alles auswählen
from timeit import default_timer
import random, collections
def measure(func):
def call_func(*args):
start = default_timer()
result = func(*args)
return result, default_timer() - start
return call_func
@measure
def with_collections(count):
return collections.Counter(random.randint(1, 6) for _ in xrange(count))
@measure
def without_collections(count):
return map([random.randint(1, 6) for _ in xrange(count)].count, xrange(1, 7))
Code: Alles auswählen
>>> with_collections(100000)
(Counter({6: 16785, 4: 16771, 2: 16731, 5: 16657, 3: 16576, 1: 16480}), 0.24344611167907715)
>>> without_collections(100000)
(<map object at 0x1754950>, 0.16588497161865234)
mutetella