Guest User

Untitled

a guest
Mar 12th, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | Source Code | 0 0
  1. from matplotlib import pyplot as plt
  2. from datetime import datetime
  3. import random
  4. import math
  5.  
  6. def r_test(n):
  7.  
  8.     intervals = []
  9.     for _ in range(n):
  10.         start = random.randint(1, n)
  11.         interval_length = random.randint(2, n)
  12.         intervals.append([start, start + interval_length])
  13.  
  14.     return intervals
  15.  
  16.  
  17. # код и треда
  18. def anon_f(input):
  19.   input = sorted(input)
  20.   result = []
  21.   while len(input) > 0:
  22.     base = input.pop(0)
  23.     while len(input) > 0 and input[0][0] <= base[1] + 1:
  24.       base = [base[0], max(base[1], input[0][1])]
  25.       input.pop(0)
  26.     result.append(base)
  27.   return result
  28.  
  29.  
  30. def nlogn(n):
  31.     for _ in range(n):
  32.         for __ in range(int(math.log(n))):
  33.                         continue
  34.     a = sum(range(n))
  35.     arr = [0] * n
  36.     for i in range(n):
  37.         arr[i] = i or 0
  38.  
  39. xs = [n for n in range(1000, 10000, 1)]
  40. ys = []
  41. ys_nlog = []
  42.  
  43. for x in xs:
  44.     a = r_test(x)
  45.     start = datetime.now()
  46.     anon_f(a)
  47.     y = (datetime.now() - start).total_seconds()
  48.     ys.append(y)
  49.    
  50.     start = datetime.now()
  51.     nlogn(x)
  52.     y = (datetime.now() - start).total_seconds()
  53.    
  54.     ys_nlog.append(y)
  55.  
  56.     print(f"x: {x} time: {y}")
  57.  
  58. plt.plot(xs, ys)
  59. plt.plot(xs, ys_nlog)
  60. plt.grid()
  61. plt.show()
  62.  
Advertisement
Add Comment
Please, Sign In to add comment