<div dir="ltr"><div><div><div><div><div><div><div><div class="gmail_quote">On Sat, Sep 14, 2013 at 9:28 PM, Raymond Hettinger <span dir="ltr"><<a href="mailto:raymond.hettinger@gmail.com" target="_blank">raymond.hettinger@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div class="im"><br></div><div><div>It is surprising that you saw any performance gain at all.</div>
<div><br></div><div>Python already has a default Python freelist scheme </div><div>in the _PyObject_Malloc() function in Objects/obmalloc.c.</div><div><br></div><div>Another thought is that this isn't an inner-loop optimization.</div>
<div>The O(1) time for iterator creation is dominated by the O(n)</div><div>time to actually iterate over the dict keys, values, and items.</div><span class=""><font color="#888888"><div><br></div><div>Raymond</div></font></span></div>
</div></blockquote></div><br><br>Hi Raymond,<br><br></div>Taking a look at _PyObject_Malloc in Objects/obmalloc.c, I see that it needs to do some lock and unlock operations.  Perhaps it's the avoidance of this overhead that I'm seeing?  After all, there must be a reason that dict, tuple and others are keeping their own free lists, right?<br>
<br></div>I'm curious what the overhead in creating the iterator is compared to the time to iterate.  Obviously there's an O(1) / O(n) difference, but perhaps the constant time outweighs smaller values of n?  In our case, we are often doing something like the following (2.7):<br>
<br></div>def onNewData(datapoints):<br></div>    for dp in datapoints:<br></div>        for val in dp.outputs.itervalues():<br></div>            # Do things with val<br></div><div>        for status in dp.statuses.itervalues():<br>
</div><div>            # Do things with status<br><br></div><div>Where datapoints can have 100000 items and "outputs" and "statuses" tend to be small.  So, while creating the iterator obviously isn't the slowest part of the code, it does have some impact.<br>
</div><div><div><div><div><div><div><div><br></div><div>Cheers,<br></div><div>-Kyle<br><br></div><div>P.S. - I'm a newbie to the mailing list, so if I'm replying "wrong" sorry about that!<br></div></div>
</div></div></div></div></div></div>