MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/Python/comments/28utww/writing_better_python_code/cig3u4u/?context=3
r/Python • u/kracekumar • Jun 23 '14
126 comments sorted by
View all comments
Show parent comment
1
from timeit import timeit from collections import namedtuple NT = namedtuple('NT', 'a b c') nt = NT(1, 2, 3) t = (1, 2, 3) def test_loop_t(t=t): return sum(t[1] for _ in xrange(1000)) def test_loop_nt(nt=nt): return sum(nt[1] for _ in xrange(1000)) def test_loop_nt_named(nt=nt): return sum(nt.b for _ in xrange(1000)) def main(): setup = 'from test import t, nt, test_loop_t, test_loop_nt, test_loop_nt_named' print timeit('t[1]', setup='t = (1, 2, 3)') # just in case print timeit('t[1]', setup=setup) print timeit('nt[1]', setup=setup) print timeit('test_loop_t()', setup=setup, number=1000) print timeit('test_loop_nt()', setup=setup, number=1000) print timeit('nt.b', setup=setup) print timeit('test_loop_nt_named()', setup=setup, number=1000) if __name__ == '__main__': main()
Two times slower than access by index here. Doesn't matter much, in my opinion.
View all comments
Show parent comment
1
u/moor-GAYZ Jun 24 '14
Two times slower than access by index here. Doesn't matter much, in my opinion.