<div dir="ltr"><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra">class Counter(object):</div><div class="gmail_extra"> __slots__ = ('gen', '_counter')</div>
<div class="gmail_extra"><br></div><div class="gmail_extra"> def __init__(self, gen):</div><div class="gmail_extra"> self.gen = gen</div><div class="gmail_extra"><br></div><div class="gmail_extra"> def count(self):</div>
<div class="gmail_extra"> return self._counter</div><div class="gmail_extra"><br></div><div class="gmail_extra"> def __iter__(self):</div><div class="gmail_extra"> c = 0</div><div class="gmail_extra"> for k in self.gen:</div>
<div class="gmail_extra"> c += 1</div><div class="gmail_extra"> yield(k)</div><div class="gmail_extra"> self._counter = float(c)</div></div></div><div class="gmail_extra"><br></div><div class="gmail_extra">
<div class="gmail_extra">def avg7():</div><div class="gmail_extra"> x=Counter(gen(10000))</div><div class="gmail_extra"> return sum(x)/x.count()</div></div><div class="gmail_extra"><br></div><div class="gmail_extra">
<br></div><div class="gmail_extra"><div class="gmail_extra">avg1 2.8361890316 50000.0</div><div class="gmail_extra">avg2 2.62487697601 50000.0</div><div class="gmail_extra">avg3 4.14600801468 50000.0</div><div class="gmail_extra">
avg4 2.05153393745 50000.0</div><div class="gmail_extra">avg5 3.35745310783 50000</div><div class="gmail_extra">avg6 1.26089906693 50000</div><div class="gmail_extra">avg7 2.20352196693 50000.0</div></div><div class="gmail_extra">
<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">meglio rispetto alla avg5 è 1/3 in meno.</div><div class="gmail_extra"><br></div></div></div>