Guest User

Untitled

a guest
May 25th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # Name: module1
  3. # Purpose:
  4. #
  5. # Author: Nir
  6. #
  7. # Created: 13/01/2012
  8. # Copyright: (c) Nir 2012
  9. # Licence: <your licence>
  10. #-------------------------------------------------------------------------------
  11. #!/usr/bin/env python
  12.  
  13. 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]
  14. filter_list = [lambda a: True, lambda a: a%2==0, lambda a:abs(a)<50, lambda a:a**2<100]
  15.  
  16. def niri(print_result=False):
  17. def filterCombine(a):
  18. for filt in filter_list:
  19. if not filt(a):
  20. return False
  21. return True
  22.  
  23. # compare only the subset that is possibly similar
  24. l=[ each for each in items if filterCombine(each)]
  25. if (print_result):
  26. print l
  27.  
  28. def ido(print_result=False):
  29.  
  30. # compare only the subset that is possibly similar
  31. l=list(filter(lambda x: all(func(x) for func in filter_list) and x or False, items))
  32. if (print_result):
  33. print l
  34.  
  35. def eran(print_result=False):
  36. l =reduce(lambda x,filt: filter(filt,x),filter_list,items)
  37. if (print_result):
  38. print l
  39.  
  40.  
  41.  
  42. if __name__ == '__main__':
  43. import time
  44. t=time.time()
  45. for i in range(100000):
  46. niri()
  47. print("time for niri: %f" % (time.time()-t))
  48.  
  49. t=time.time()
  50. for i in range(100000):
  51. ido()
  52. print("time for ido: %f" % (time.time()-t))
  53.  
  54. t=time.time()
  55. for i in range(100000):
  56. eran()
  57. print("time for eran: %f" % (time.time()-t))
  58.  
  59. niri(True)
  60. ido(True)
  61. eran(True)
  62.  
  63.  
  64. --------------------------------------------------------------------------------
  65. Output
  66. --------------------------------------------------------------------------------
  67. time for niri: 3.481000
  68. time for ido: 6.973000
  69. time for eran: 1.919000
  70. [2, 4, 2, 6, 4, 6, 2, 2, 4, 4, 6, 6, 6, 4]
  71. [2, 4, 2, 6, 4, 6, 2, 2, 4, 4, 6, 6, 6, 4]
  72. [2, 4, 2, 6, 4, 6, 2, 2, 4, 4, 6, 6, 6, 4]
Add Comment
Please, Sign In to add comment