What do you guys think?
Completely unscientific microbenchmarks: (gist)
>>> from timeit import Timer
## creation
# tuple
>>> Timer('(1, 2, 3, 4, 5)').timeit()
0.02694106101989746
# namedtuple with args
>>> Timer('T(1, 2, 3, 4, 5)', setup='''
from collections import namedtuple
T = namedtuple("T", "a b c d e")'''
).timeit()
0.773979902267456
# namedtuple with kwargs
>>> Timer('T(a=1, b=2, c=3, d=4, e=5)', setup='''
from collections import namedtuple
T = namedtuple("T", "a b c d e")'''
).timeit()
1.155663013458252
## item access
# tuple
>>> Timer('t[0], t[1], t[2], t[3], t[4]', setup='t = (1, 2, 3, 4, 5)').timeit()
0.3240630626678467
# namedtuple with indices
>>> Timer('t[0], t[1], t[2], t[3], t[4]', setup='''
from collections import namedtuple
T = namedtuple("T", "a b c d e")
t = T(1, 2, 3, 4, 5)'''
).timeit()
0.2994410991668701
# namedtuple with attributes
>>> Timer('t.a, t.b, t.c, t.d, t.e', setup='''
from collections import namedtuple
T = namedtuple("T", "a b c d e")
t = T(1, 2, 3, 4, 5)'''
).timeit()
0.9363529682159424
It seems that the only significant slowdown is on a namedtuple's creation. I imagine the impact would be negligible on a complete request-response cycle, but that would have to be tested.
Accessing a namedtuple's items using attributes is also somewhat slower, but this wouldn't be a problem. Existing code would continue to work with the same performance and users could decide for themselves which way to access a namedtuple's items for new code.
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/61b1a5f0-1d1c-480d-87cd-639c728327a7%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/699D6AD0-A3F2-425A-805D-8376098707B4%40gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAJxq84_Q4GgqYN44FJXz9z58Wk%2B_0rvhCrFiz9Tkee4kucko-w%40mail.gmail.com.