Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # проверка производительности кода создания списков разными способами
- # src: https://pythonist.ru/predstavlenie-spiskov-v-yazyke-python/
- from datetime import datetime as dt
- def variant1(limit):
- l1 = [x ** 2 for x in range(1, limit) if x % 2 == 0]
- def variant2(limit):
- sqr = []
- for x in range(1, limit):
- if x % 2 == 0:
- sqr.append(x ** 2)
- def variant3(limit):
- l0 = list(map(lambda x: x ** 2, filter(lambda x: x % 2 == 0, range(1, limit))))
- if __name__ == '__main__':
- limit = 10 ** 8
- timestart = dt.now()
- variant1(limit)
- timeend = dt.now()
- print(f"variant 1 {timeend - timestart}")
- timestart = dt.now()
- variant2(limit)
- timeend = dt.now()
- print(f"variant 2 {timeend - timestart}")
- timestart = dt.now()
- variant3(limit)
- timeend = dt.now()
- print(f"variant 3 {timeend - timestart}")
- """
- Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
- uname -a
- Linux arrow 5.6.0-kali2-amd64 #1 SMP Debian 5.6.14-1kali1 (2020-05-25) x86_64 GNU/Linux
- variant 1 0:00:12.477574
- variant 2 0:00:13.516124
- variant 3 0:00:18.117899
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement