Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from timeit import timeit
- def setup():
- cities_hab = dict({
- "Chicago": 9_526_434,
- "Londre": 14_257_962,
- "Lyon": 513_275,
- "Xi'an": 8_467_837,
- })
- return cities_hab
- def do_nothing(name):
- pass
- def without_lc(cities_hab):
- for city_name in cities_hab.keys():
- do_nothing(city_name)
- def with_lc(cities_hab):
- [do_nothing(city_name) for city_name in cities_hab.keys()]
- def with_map(cities_hab):
- map(do_nothing, cities_hab.keys())
- if __name__ == '__main__':
- t1 = timeit('without_lc(cities_hab)',
- setup='from __main__ import without_lc, do_nothing, setup; cities_hab = setup()',
- number=100000)
- t2 = timeit('with_lc(cities_hab)',
- setup='from __main__ import with_lc, do_nothing, setup; cities_hab = setup()',
- number=100000)
- t3 = timeit('with_map(cities_hab)',
- setup='from __main__ import with_map, do_nothing, setup; cities_hab = setup()',
- number=100000)
- print(t1, t2, t3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement