[Python-ideas] Yield-from: More timings

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Feb 22 23:52:58 CET 2009


I neglected to turn off cyclic gc while performing
the timings. Doing so, the overhead is measured
at about 5%.

I've also tried some experiments with traversing a
binary tree. For very small trees the yield-from
version starts off slightly slower overall than the
for-loop version. Break-even occurs at about 31
nodes in the tree, and after that the yield-from
version steadily gains ground, until at 1 million
nodes it's 1.6 times faster.

The columns below are: depth of the tree, for-loop
time, yield-from time and the ratio of the times
(> 1 meaning yield-from is faster).

   1 0.000049 0.000060 0.805263
   2 0.000051 0.000072 0.706534
   3 0.000069 0.000108 0.637564
   4 0.000130 0.000148 0.878364
   5 0.000265 0.000253 1.04675
   6 0.000457 0.000458 0.997223
   7 0.000988 0.000920 1.07379
   8 0.002025 0.001743 1.162
   9 0.003985 0.003437 1.15947
  10 0.008345 0.006848 1.2185
  11 0.017398 0.013902 1.25145
  12 0.036223 0.027844 1.30091
  13 0.075175 0.055327 1.35874
  14 0.154206 0.110290 1.39819
  15 0.318121 0.220421 1.44324
  16 0.663929 0.442529 1.50031
  17 1.369077 0.891843 1.53511
  18 2.827645 1.798144 1.57254
  19 5.883996 3.661280 1.60709
  20 12.093364 7.424631 1.62882

def forloop(node):
   if node == 1:
     yield node
   else:
     for x in forloop(node[0]):
       yield x
     for x in forloop(node[1]):
       yield x

def yieldfrom(node):
   if node == 1:
     yield node
   else:
     yield from yieldfrom(node[0])
     yield from yieldfrom(node[1])

--
Greg




More information about the Python-ideas mailing list