<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jul 19, 2017 at 5:20 PM, Tim Peters <span dir="ltr"><<a href="mailto:tim.peters@gmail.com" target="_blank">tim.peters@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">[Giampaolo Rodola' <<a href="mailto:g.rodola@gmail.com" target="_blank">g.rodola@gmail.com</a>>]<br>
<span>> Still much slower (-4.3x) than plain tuples though:<br>
><br>
> $ python3.7 -m timeit -s "import collections; Point => collections.namedtuple('Point'<wbr>, ('x', 'y'));" "Point(5, 11)"<br>
> 1000000 loops, best of 5: 313 nsec per loop<br>
><br>
> $ python3.7 -m timeit "tuple((5, 11))"<br>
> 5000000 loops, best of 5: 71.4 nsec per loop<br>
<br>
</span>I believe this was pointed out earlier: in the second case,<br>
<br>
1. (5, 11) is built at _compile_ time, so at runtime it's only<br>
measuring a LOAD_FAST to fetch it from the code's constants block.<br>
<br>
2. The tuple() constructor does close to nothing when passed a tuple:<br>
it just increments the argument's reference count and returns it.<br>
<br>
>>> t = (1, 2)<br>
>>> tuple(t) is t<br>
True<br>
<br>
In other words, the second case isn't measuring tuple _creation_ time<br>
in any sense: it's just measuring how long it takes to look up the<br>
name "tuple" and increment the refcount on a tuple that was created at<br>
compile time.<br>
</blockquote></div><br>Oh right, I didn't realize that, sorry. Should have been something like this instead:</div><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra"></div><div><br></div><div><div>$ python3.7 -m timeit -s "import collections; Point = collections.namedtuple('Point', ('x', 'y')); x = [5, 1]" "Point(*x)"</div><div>1000000 loops, best of 5: 311 nsec per loop</div><div><br></div><div>$ python3.7 -m timeit -s "x = [5, 1]" "tuple(x)"</div><div>5000000 loops, best of 5: 89.8 nsec per loop</div></div><div><br></div></div>-- <br><div class="gmail-m_7817433277338961534gmail_signature"><div dir="ltr"><div>Giampaolo - <a href="http://grodola.blogspot.com" target="_blank">http://grodola.blogspot.com</a></div><div><br></div></div></div>
</div></div>