Anand Balachandran Pillai <abpil...@gmail.com> writes: > On Mon, Aug 1, 2011 at 6:08 AM, Anand Chitipothu <anandol...@gmail.com>wrote:
[...] > It is more subtler than that. > > List comprehensions are faster than map functions when > the latter needs to invoke a user-defined function call or a lambda. > > Maps score over list comprehensions in most cases where the function > is a Python built-in and when no lambda is used. > > Example of former: > >>>> def f1(): map(sqr, range(1, 100)) > ... >>>> def f2(): [sqr(x) for x in range(1, 100)] > ... >>>> mytimeit.Timeit(f1) > '37.91 usec/pass' >>>> mytimeit.Timeit(f2) > '37.50 usec/pass' > > Example of latter: > >>>> def f1(): map(hex, range(1, 100)) > ... >>>> def f2(): [hex(x) for x in range(1, 100)] > ... >>>> mytimeit.Timeit(f1) > '49.41 usec/pass' >>>> mytimeit.Timeit(f2) > '55.29 usec/pass' This is confusing. Why is map(sqr, range(1, 100)) faster than map(hex, range(1, 100)) Assuming sqr is implemented in python, it should be slower than hex which is implemented in C. [...] -- ~noufal http://nibrahim.net.in She used to diet on any kind of food she could lay her hands on. -- Arthur Baer, American comic and columnist _______________________________________________ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers