which one is faster np.vstack, np.append, np.concatenate or a manual function made in cython?
我编写了一些程序,该程序在每次迭代中都会更新
我已经实现了一个基本算法,我不确定这是否是计算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import numpy as np cdef int[:, :] pl_list cdef list pl_length cdef list bonus pl_list = np.array([[8, 7]], dtype=np.int32) def modify(pl_list, pl_length): cdef int k_const = 10 mean = np.mean(pl_list, axis=0) mean = np.subtract(mean, pl_length) dev = np.std(pl_list, axis=0) mean[0] / dev[0] if dev[0] != 0 else 0 mean[1] / dev[1] if dev[1] != 0 else 0 bonus = -1 + (2 / (1 + np.exp(-k_const * mean))) return list(bonus) for i in range(2499): # I just simplified the loop. the main loop works like startTime - time.clock() < seconds rand = np.random.randint(8, 64) pl_length = [rand, rand-1] pl_list = np.append(pl_list, [pl_length], axis=0) bonus = modify(pl_list, pl_length) |
我当时正在考虑使用以下思路来加速该程序:
使用自制函数来计算np.std,np.mean(因为在cython中在内存视图中迭代如此之快):
我还考虑为内存视图定义一个静态长度(例如2500),所以我不需要使用
对不起,如果我的问题太多且太复杂。我只是想在速度上获得最佳性能。
忽略
1 2 3 4 5 6 7 | pl_list = np.array([[8, 7]], dtype=np.int32) .... for i in range(2499): .... pl_list = np.append(pl_list, [pl_length], axis=0) ... |
作为一般规则,我们不建议在循环中使用
查看诸如
另一个模型是分配一个足够大的数组作为开始,例如