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, city_hab in cities_hab.items():
- if city_hab >= 1_000_000:
- do_nothing(city_name)
- def with_lc(cities_hab):
- [do_nothing(city_name) for city_name, city_hab in cities_hab.items()
- if city_hab >= 1_000_000]
- 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)
- print(t1, t2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement