Advertisement
VikkaLorel

list comprehension perf without filter

Jan 20th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. from timeit import timeit
  2.  
  3.  
  4. def setup():
  5.     cities_hab = dict({
  6.         "Chicago": 9_526_434,
  7.     "Londre": 14_257_962,
  8.     "Lyon": 513_275,
  9.     "Xi'an": 8_467_837,
  10.         })
  11.     return cities_hab
  12.  
  13.  
  14. def do_nothing(name):
  15.     pass
  16.  
  17. def without_lc(cities_hab):
  18.     for city_name in cities_hab.keys():
  19.         do_nothing(city_name)
  20.  
  21.  
  22. def with_lc(cities_hab):
  23.     [do_nothing(city_name) for city_name in cities_hab.keys()]
  24.  
  25.  
  26. def with_map(cities_hab):
  27.     map(do_nothing, cities_hab.keys())
  28.  
  29.  
  30. if __name__ == '__main__':
  31.     t1 = timeit('without_lc(cities_hab)',
  32.                 setup='from __main__ import without_lc, do_nothing, setup; cities_hab = setup()',
  33.                 number=100000)
  34.     t2 = timeit('with_lc(cities_hab)',
  35.                 setup='from __main__ import with_lc, do_nothing, setup; cities_hab = setup()',
  36.                 number=100000)
  37.     t3 = timeit('with_map(cities_hab)',
  38.                 setup='from __main__ import with_map, do_nothing, setup; cities_hab = setup()',
  39.                 number=100000)
  40.     print(t1, t2, t3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement