我正在尝试生成一个简单的图表来显示 O(n^2) 超过一千个项目。代码似乎工作得很好,但是当我使用 matplotlib 绘制它时,图形以线性方式出现,这意味着 y 的图形似乎等于 x。我希望有一个像这样的漂亮的 O(n^2) 图:
我正在运行我的基础超过 10000 个项目,它仍然是线性的吗?
这是我正在运行的:
import time
import matplotlib.pyplot as plt
counter = 0
start_time = time.perf_counter()
N = 10000
timings = []
x = []
y = []
for i in range(0, N):
for j in range(0, N):
# do something
pass
step_time = time.perf_counter() - start_time
timings.append([counter, round(step_time, 2)])
counter += 1
# NOT efficient code!
for t in timings:
x.append(t[0])
y.append(t[1])
plt.xlabel('number of points')
plt.ylabel('time')
plt.plot(x, y)
plt.title('Quadratic Graph!')
plt.show()