Advertisement
sphinx2001

тест производительности работы со списками

Jun 10th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. # проверка производительности кода создания списков разными способами
  2. # src: https://pythonist.ru/predstavlenie-spiskov-v-yazyke-python/
  3. from datetime import datetime as dt
  4.  
  5.  
  6. def variant1(limit):
  7.     l1 = [x ** 2 for x in range(1, limit) if x % 2 == 0]
  8.  
  9.  
  10. def variant2(limit):
  11.     sqr = []
  12.     for x in range(1, limit):
  13.         if x % 2 == 0:
  14.             sqr.append(x ** 2)
  15.  
  16.  
  17. def variant3(limit):
  18.     l0 = list(map(lambda x: x ** 2, filter(lambda x: x % 2 == 0, range(1, limit))))
  19.  
  20.  
  21. if __name__ == '__main__':
  22.     limit = 10 ** 8
  23.     timestart = dt.now()
  24.     variant1(limit)
  25.     timeend = dt.now()
  26.     print(f"variant 1 {timeend - timestart}")
  27.  
  28.     timestart = dt.now()
  29.     variant2(limit)
  30.     timeend = dt.now()
  31.     print(f"variant 2 {timeend - timestart}")
  32.  
  33.     timestart = dt.now()
  34.     variant3(limit)
  35.     timeend = dt.now()
  36.     print(f"variant 3 {timeend - timestart}")
  37.  
  38. """
  39. Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  40. uname -a
  41. Linux arrow 5.6.0-kali2-amd64 #1 SMP Debian 5.6.14-1kali1 (2020-05-25) x86_64 GNU/Linux
  42.  
  43. variant 1 0:00:12.477574
  44. variant 2 0:00:13.516124
  45. variant 3 0:00:18.117899
  46. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement