<div dir="ltr"><div class="gmail_extra"><div><div><div><div>Qualche prova in più:</div></div></div></div><div><br></div><div><div><font face="courier new, monospace">import itertools</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace">def gen(n):</font></div><div><font face="courier new, monospace"> i = 1</font></div><div><font face="courier new, monospace"> while i < n:</font></div><div><font face="courier new, monospace"> yield i</font></div>
<div><font face="courier new, monospace"> i += 1</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">def avg1():</font></div><div><font face="courier new, monospace"> n, sum_ = 0, 0.0</font></div>
<div><font face="courier new, monospace"> for v in gen(100000):</font></div><div><font face="courier new, monospace"> sum_ += v</font></div><div><font face="courier new, monospace"> n += 1</font></div><div>
<font face="courier new, monospace"> return sum_ / n</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">def avg2():</font></div><div><font face="courier new, monospace"> tot = 0.0</font></div>
<div><font face="courier new, monospace"> for n, v in enumerate(gen(100000), 1):</font></div><div><font face="courier new, monospace"> tot += v</font></div><div><font face="courier new, monospace"> try:</font></div>
<div><font face="courier new, monospace"> return tot / n</font></div><div><font face="courier new, monospace"> except UnboundLocalError:</font></div><div><font face="courier new, monospace"> return 0 # ???</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">def avg3():</font></div><div><font face="courier new, monospace"> x = itertools.izip(*((i, 1) for i in gen(100000)))</font></div>
<div><font face="courier new, monospace"> return float(sum(x.next())) / sum(x.next())</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div><div>
<font face="courier new, monospace">def avg4():</font></div><div><font face="courier new, monospace"> it1, it2 = itertools.tee(gen(100000))</font></div><div><font face="courier new, monospace"> try:</font></div><div>
<font face="courier new, monospace"> return sum(it1) / float(sum(1 for _ in it2))</font></div><div><font face="courier new, monospace"> except ZeroDivisionError:</font></div><div><font face="courier new, monospace"> return 0 # ???</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">if __name__ == '__main__':</font></div><div><font face="courier new, monospace"> import timeit</font></div><div>
<font face="courier new, monospace"> for s in range(1, 5):</font></div><div><font face="courier new, monospace"> f = "avg%s" % s</font></div><div><font face="courier new, monospace"> print f, timeit.timeit(f + "()", setup="from __main__ import %s" % f, number=100)</font></div>
</div><div><br></div><div><br></div><div><div><font face="courier new, monospace"><b>avg1 2.8069589138</b></font></div><div><font face="courier new, monospace"><b>avg2 2.88984298706</b></font></div><div><font face="courier new, monospace"><b>avg3 6.68095088005</b></font></div>
<div><font face="courier new, monospace"><b>avg4 2.85471391678</b></font></div></div><div><br></div><div>Postmettendo che avg1 e avg3 nel caso di sequenza nulla sbagliano.</div><div><br></div><div>Ciao.</div><div>Marco.</div>
<div><br></div>
</div></div>