Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-------------------------------------------------------------------------------
- # Name: module1
- # Purpose:
- #
- # Author: Nir
- #
- # Created: 13/01/2012
- # Copyright: (c) Nir 2012
- # Licence: <your licence>
- #-------------------------------------------------------------------------------
- #!/usr/bin/env python
- items = [1,2,4,56,67,3,2,5,6,7,43,34,4,6,7,3,2,2,3,4,4,333,45,6,5,4456,6,5,5,678,76,5,44,6778,654567,65,67,6,546,7,65,4,5]
- filter_list = [lambda a: True, lambda a: a%2==0, lambda a:abs(a)<50, lambda a:a**2<100]
- def niri(print_result=False):
- def filterCombine(a):
- for filt in filter_list:
- if not filt(a):
- return False
- return True
- # compare only the subset that is possibly similar
- l=[ each for each in items if filterCombine(each)]
- if (print_result):
- print l
- def ido(print_result=False):
- # compare only the subset that is possibly similar
- l=list(filter(lambda x: all(func(x) for func in filter_list) and x or False, items))
- if (print_result):
- print l
- def eran(print_result=False):
- l =reduce(lambda x,filt: filter(filt,x),filter_list,items)
- if (print_result):
- print l
- if __name__ == '__main__':
- import time
- t=time.time()
- for i in range(100000):
- niri()
- print("time for niri: %f" % (time.time()-t))
- t=time.time()
- for i in range(100000):
- ido()
- print("time for ido: %f" % (time.time()-t))
- t=time.time()
- for i in range(100000):
- eran()
- print("time for eran: %f" % (time.time()-t))
- niri(True)
- ido(True)
- eran(True)
- --------------------------------------------------------------------------------
- Output
- --------------------------------------------------------------------------------
- time for niri: 3.481000
- time for ido: 6.973000
- time for eran: 1.919000
- [2, 4, 2, 6, 4, 6, 2, 2, 4, 4, 6, 6, 6, 4]
- [2, 4, 2, 6, 4, 6, 2, 2, 4, 4, 6, 6, 6, 4]
- [2, 4, 2, 6, 4, 6, 2, 2, 4, 4, 6, 6, 6, 4]
Add Comment
Please, Sign In to add comment