Re[2]: [Tutor] map, filter and lambda functions

Kerim Borchaev Kerim Borchaev" <warkid@storm.ru
Thu, 23 Aug 2001 13:15:15 +0400


Yep. On my W2k/PII450 these are much faster too.

And here's a classical "optimization anecdote" that worth to read if
one want's to know more about "what is efficient" :
http://www.python.org/doc/essays/list2str.html

Best regards,
 Kerim                            mailto:warkid@storm.ru

Thursday, August 23, 2001, 12:58:07 PM, you wrote:

>>===== Original Message From "Kerim Borchaev" <warkid@storm.ru> =====
>>Thursday, August 23, 2001, 9:23:50 AM, you wrote:
>>
>>RR> checklist = string.split(filetext, '\n')
>>RR> checklist = filter(None, checklist)
>>RR> checklist = map(string.strip, checklist)
>>RR> checklist = map(string.upper, checklist)
>>
>>RR> If checklist is rather long, the last two may be written mor efficiently
>>RR> as:
>>
>>RR> checklist = map(lambda x:string.upper(string.strip(x)), checklist)
>>
>>About efficiency - running the script below i get this:
>>
>>2.01
>>1.32
>>2.01
>>1.32
>>
>>no luck for lambda...

HN> On my box, these two are faster:

HN>  print "trial 5:",
HN>  def popo(x):
HN>      return x.strip().upper()
HN>  start = time.clock()
HN>  for i in range(num_iter):
HN>      checklist=l
HN>      checklist = map(popo, checklist)
HN>  stend = time.clock()
HN>  print "%.2f"%(stend-start)

HN>  print "trial 6:",
HN>  start = time.clock()
HN>  for i in range(num_iter):
HN>      checklist=l
HN>      checklist = [x.strip().upper() for x in checklist]
HN>  stend = time.clock()
HN>  print "%.2f"%(stend-start)

HN> I'm on an overloaded NT box right now, and the results vary horribly for each 
HN> run, but 5 and 6 are consistently faster than the rest.

HN> Mzzl,

HN> --Hans Nowak


HN> _______________________________________________
HN> Tutor maillist  -  Tutor@python.org
HN> http://mail.python.org/mailman/listinfo/tutor