#%% md
时间复杂度和空间复杂度
这是任何AI工程师必须要深入理解的概念。对于每一个设计出来的算法都需要从这两个方面来分析
O(N), O(N^2) : o notation
#%%
int a = 0, b = 0;
for (i = 0; i < N; i++) { # O(N)+O(N)=2*O(N)=O(N)
a = a + rand();# N*1个操作 = O(N)
b = b + rand();# N*1个操作 = O(N)
}
for (j = 0;
快:时间复杂度
省:空间复杂度
1. 时间复杂度分析
1.1 实例1
def get_sum(n):
result = 0
for i in range(1,n+1):
result += i
return result
假设每行代码对应的cpu执行时间一样,为一个时间单位unit_time
可以看到该函数第2、5行执行1次,第3、4行执行n次
总共执行时间为:
T(n)=2 * unit_time+2 * n * unit_time=(2+2n) *