impressive_i

Профилирование циклов в Python

Jan 26th, 2021 (edited)
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. # Примеры работы циклов for и while для выполнения
  2. # одной и той же задачи. Время выполнение циклов
  3.  
  4.  
  5. a1 = 3  # Первый член арифметической прогрессии
  6. d = 4   # Разница между соседними членами арифметической прогрессии
  7. N = 6   # Количество членов арифметической прогрессии
  8.  
  9. # Поиск суммы через цикл
  10. S_circle = 0  # Сумма подсчитанная перебором через цикл
  11. for k in range(1, N + 1):
  12.     ak = a1 + ( k - 1 )*d
  13.     S_circle = S_circle + ak
  14.  
  15. print('Сумма арифметической прогрессии через цикл: ', S_circle)
  16.  
  17.  
  18. # Поиск суммы через теоретическую формулу
  19. S_general = 0.5*(2*a1 + (N-1)*d)*N  # Сумма подсчитанная перебором через цикл
  20. print('Сумма арифметической прогрессии через формулу: ', S_general)
  21.  
  22.  
  23. for k in range(0, 10):
  24.     print('for: Итерация №',k)
  25.  
  26. k = 0
  27. while(k < 10):
  28.     print('while: Итерация №',k)
  29.     k += 1
  30.  
  31. # Попытка профилирования кода
  32. import timeit
  33.  
  34. code_for = '''
  35. numberOfElements = 4
  36. amount = 0
  37. for currentElement in range(1, numberOfElements + 1):
  38.    amount = amount + 1 / currentElement
  39. '''
  40.  
  41. code_while = '''
  42. currentElement = 1
  43. numberOfElements = 4
  44. amount = 0
  45. while(currentElement <= numberOfElements):
  46.    amount = amount + 1 / currentElement
  47.    currentElement = currentElement + 1
  48. '''
  49.  
  50. execution_time_for = timeit.timeit(code_for, number = 1000000)
  51. execution_time_while = timeit.timeit(code_while, number = 1000000)
  52.  
  53. print('Время выполнения цикла FOR: ', execution_time_for)
  54. print('Время выполнения цикла WHILE: ', execution_time_while)
  55.  
  56. # Примеры работы
  57.  
  58. currentElement = 1
  59. numberOfElements = 4
  60. amount = 0
  61. while(currentElement <= numberOfElements):
  62.     amount = amount + 1 / currentElement
  63.     currentElement = currentElement + 1
  64.  
  65. numberOfElements = 4
  66. amount = 0
  67. for currentElement in range(1, numberOfElements + 1):
  68.     amount = amount + 1 / currentElement
  69.  
  70. print('while: Сумма первых {} \
  71. членов ряда равна: {}'.format(numberOfElements, amount))
  72. print('for: Сумма первых {} \
  73. членов ряда равна: {}'.format(numberOfElements, amount))
  74.  
  75.  
  76. print('Дисассемблирование:')
  77.  
  78. import dis
  79.  
  80. def test_for():
  81.     numberOfElements = 4
  82.     amount = 0
  83.     for currentElement in range(1, numberOfElements + 1):
  84.         amount = amount + 1 / currentElement
  85.    
  86. def test_while():
  87.     currentElement = 1
  88.     numberOfElements = 4
  89.     amount = 0
  90.     while(currentElement <= numberOfElements):
  91.         amount = amount + 1 / currentElement
  92.         currentElement = currentElement + 1
  93.  
  94. print('Asm FOR:')
  95. print(dis.dis(test_for))
  96. print('Asm WHILE:')
  97. print(dis.dis(test_while))
Add Comment
Please, Sign In to add comment