<div dir="ltr"><div><font face="courier new,monospace"> </font></div><div><font face="courier new,monospace"> </font></div><div><font face="courier new,monospace">On Thu, Jul 18, 2013 at 11:59 PM, Nick Coghlan <<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>> wrote:</font></div>
<div><font face="courier new,monospace">></font></div><div><font face="courier new,monospace">> On 19 July 2013 00:29, Ron Adam <<a href="mailto:ron3200@gmail.com">ron3200@gmail.com</a>> wrote:</font></div><div>
<font face="courier new,monospace">> > I was thinking a generator would be more efficient if it's called many</font></div><div><font face="courier new,monospace">> > times.  But I think this is easier to understand.  If there is more interest</font></div>
<div><font face="courier new,monospace">> > we can test both to see how much of a difference it makes.</font></div><div><font face="courier new,monospace">></font></div><div><font face="courier new,monospace">> Suspending and resuming a generator is quite an expensive operation.</font></div>
<div><font face="courier new,monospace">> send() has the triple whammy of method call + resume generator +</font></div><div><font face="courier new,monospace">> suspend generator, so it's unlikely to outperform a simple method call</font></div>
<div><font face="courier new,monospace">> (even one that redirects to another method).</font></div><div><font face="courier new,monospace"> </font></div><div><font face="courier new,monospace"> </font></div><div><font face="courier new,monospace">I thought one of the things about generators was that they didn't have to create a new frame, or store attributes.   And those features would make them a bit faster than a function call.   Hmmm,  but that is wrapped in a method call.</font></div>
<div><font face="courier new,monospace"> </font></div><div><font face="courier new,monospace">I did some tests with send(), and it is about 3% slower than a function call.  Some of that difference is due to the loop in the generator.  So it's pretty close.</font></div>
<div><font face="courier new,monospace"> </font></div><div><font face="courier new,monospace">Could we have syntax for generators to bypass the method calls? </font></div><div><font face="courier new,monospace"> </font></div>
<div><font face="courier new,monospace">    x = gen[]                 # next</font></div><div><font face="courier new,monospace"> </font></div><div><font face="courier new,monospace">    gen[] = x                 # send;   or    "gen[] x"</font></div>
<div><font face="courier new,monospace"> </font></div><div><font face="courier new,monospace">    x = gen[]; gen[] = x      # or   "x = gen[] x"</font></div><div><font face="courier new,monospace"> </font></div>
<div><font face="courier new,monospace">Currently using empty brackets like this is a syntax error.   The brackets imply it is a type of iterator.  Which is the most common use for generators.</font></div><div><font face="courier new,monospace"> </font></div>
<div><font face="courier new,monospace"> </font></div><div><font face="courier new,monospace">> Independent of performance though, I think the mutable sequence API</font></div><div><font face="courier new,monospace">> inspired append(), extend() and += are a better API for what you're</font></div>
<div><font face="courier new,monospace">> trying to achieve than "send", so it doesn't make sense to me to try</font></div><div><font face="courier new,monospace">> to shoehorn this into the generator API.</font></div>
<div><font face="courier new,monospace"></font> </div><div><font face="courier new,monospace">I'm 50/50 on this right now.  So I want to test both out in different situations.  Keeping the behavior as dead simple as possible may be better because then it creates an object that would be used in a very consistent way.  I think that is one of the benefits of the simple iterator design.</font></div>
<div><font face="Courier New"></font> </div><div><font face="courier new,monospace">Ron</font></div><div> </div><div><font face="courier new,monospace"> </font><font face="courier new,monospace"> </font></div></div>