小编Col*_* Su的帖子

NumPy矩阵向量乘法的性能下降

我在NumPy矩阵向量乘法上遇到了一些(神秘的)性能问题。

我编写了以下代码段来测试矩阵向量乘法的速度:

import timeit
for i in range(90, 101):
    tm = timeit.repeat('np.matmul(a, b)', number = 10000,
        setup = 'import numpy as np; a, b = np.random.rand({0},{0}), np.random.rand({0})'.format(i))
    print(i, sum(tm) / 5)
Run Code Online (Sandbox Code Playgroud)

在某些机器上,结果是正常的:

90 0.08936462279998522
91 0.08872119059979014
92 0.09083068459967762
93 0.09311594780047017
94 0.09907015420012613
95 0.10136517100036144
96 0.10339414420013782
97 0.10627872140012187
98 0.1102267580001353
99 0.11277738099979615
100 0.11471197419996315
Run Code Online (Sandbox Code Playgroud)

在某些机器上,乘法在大小为96时变慢了:

90 0.03618830284103751
91 0.03737151022069156
92 0.03295294055715203
93 0.02851409767754376
94 0.02677299762144685
95 0.028137388220056892
96 0.1916038074065
97 0.16719966367818415
98 0.18511182265356182
99 0.1806833743583411
100 0.17172936061397195
Run Code Online (Sandbox Code Playgroud)

有些甚至减慢了1000倍:

90 0.04183819475583732 …
Run Code Online (Sandbox Code Playgroud)

python numpy

5
推荐指数
1
解决办法
87
查看次数

标签 统计

numpy ×1

python ×1