|
4
|
Fast factory funtion for Py2.5's defaultdict making simple use of itertools. Equivalent to lambda:some_constant.
Using itertools.repeat(const).next is a pure C, fast version of lambda:const. This takes full advantage of the speed and convenience benefits of the new collections.defaultdict().
Tags: shortcuts
|
2 comments
Add a comment
Sign in to comment

Download
Copy to clipboard

just wrap it up. >>> timeit.Timer('a()', 'a = lambda:"foobarbaz"').timeit() 0.75941300392150879
Surprisingly, these are all consistent results, even the last two! I really can't explain why, but creating a function you can pass something to to do the work, rather than using itertools.repeat().next yourself is actually faster on the actual calls!
Meaningful timing results. The timeit module can be tricky to use. Try specifying exactly how many repetitions you want and run the test multiple times in the a single script. Try to get your system as quiet as possible (switch-off other processes and net activity). Then, you with have a fighting chance of getting results that make sense: