Python Numpy 数据类型性能

标签 python performance types numpy benchmarking

所以我做了一些测试,得到了奇怪的结果。

代码:

import numpy as np
import timeit


setup = """
import numpy as np
A = np.ones((1000,1000,3), dtype=datatype)
"""

datatypes = "np.uint8", "np.uint16", "np.uint32", "np.uint64",  "np.float16", "np.float32", "np.float64"

stmt1 = """
A = A * 255
A = A / 255
A = A - 1
A = A + 1
"""
#~ np.uint8 : 1.04969205993
#~ np.uint16 : 1.19391073202
#~ np.uint32 : 1.37279821351
#~ np.uint64 : 2.99286961148
#~ np.float16 : 9.62375889588
#~ np.float32 : 0.884994368045
#~ np.float64 : 0.920502625252

stmt2 = """
A *= 255
A /= 255
A -= 1
A += 1
"""
#~ np.uint8 : 0.959514497259
#~ np.uint16 : 0.988570167659
#~ np.uint32 : 0.963571471946
#~ np.uint64 : 2.07768933333
#~ np.float16 : 9.40085450056
#~ np.float32 : 0.882363984225
#~ np.float64 : 0.910147440048

stmt3 = """
A = A * 255 / 255 - 1 + 1
"""
#~ np.uint8 : 1.05919667881
#~ np.uint16 : 1.20249978404
#~ np.uint32 : 1.58037744789
#~ np.uint64 : 3.47520357571
#~ np.float16 : 10.4792515701
#~ np.float32 : 1.29654744484
#~ np.float64 : 1.80735079168

stmt4 = """
A[:,:,:2] *= A[:,:,:2]
"""
#~ np.uint8 : 1.23270964172
#~ np.uint16 : 1.3260807837
#~ np.uint32 : 1.32571002402
#~ np.uint64 : 1.76836543305
#~ np.float16 : 2.83364821535
#~ np.float32 : 1.31282323872
#~ np.float64 : 1.44151875479

stmt5 = """
A[:,:,:2] = A[:,:,:2] * A[:,:,:2]
"""
#~ np.uint8 : 1.38166223494
#~ np.uint16 : 1.49569114821
#~ np.uint32 : 1.53105315419
#~ np.uint64 : 2.03457943366
#~ np.float16 : 3.01117795524
#~ np.float32 : 1.51807271679
#~ np.float64 : 1.7164808877

stmt6 = """
A *= 4
A /= 4
"""
#~ np.uint8 : 0.698176392658
#~ np.uint16 : 0.709560468038
#~ np.uint32 : 0.701653066443
#~ np.uint64 : 1.64199069295
#~ np.float16 : 4.86752675499
#~ np.float32 : 0.421001675475
#~ np.float64 : 0.433056710408

stmt7 = """
np.left_shift(A, 2, A)
np.right_shift(A, 2, A)
"""
#~ np.uint8 : 0.381521115341
#~ np.uint16 : 0.383545967785
#~ np.uint32 : 0.386147272415
#~ np.uint64 : 0.665969478824


for stmt in [stmt1, stmt2, stmt3, stmt4, stmt5, stmt6, stmt7]:
    print stmt
    for d in datatypes:
        s = setup.replace("datatype", d)
        T = timeit.Timer(stmt=stmt, setup=s)
        print d,":", min(T.repeat(number=30))
    print
print

为什么 float16 这么慢? 为什么 float32 这么快?它通常比整数运算更快。

如果您有任何相关的性能提示,我会很高兴听到它们。

这是 windows 8 64 位上的 python 2.6.6 32 位。 Numpy 1.6、Numpy 1.7 的数字类似。现在将测试 MKL 优化版本:http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy

编辑:事实证明,MKL 版本在某些浮点情况下稍快,但有时对于整数运算要慢得多:

stmt2 = """
A *= 255
A /= 255
A -= 1
A += 1
"""
#np1.6
#~ np.uint8 : 0.959514497259
#~ np.uint16 : 0.988570167659
#~ np.uint32 : 0.963571471946
#~ np.uint64 : 2.07768933333
#~ np.float16 : 9.40085450056
#~ np.float32 : 0.882363984225
#~ np.float64 : 0.910147440048

# np1.7
#~ np.uint8 : 0.979
#~ np.uint16 : 1.010
#~ np.uint32 : 0.972
#~ np.uint64 : 2.081
#~ np.float16 : 9.362
#~ np.float32 : 0.882
#~ np.float64 : 0.918

# np1.7 mkl
#~ np.uint8 : 1.782
#~ np.uint16 : 1.145
#~ np.uint32 : 1.265
#~ np.uint64 : 2.088
#~ np.float16 : 9.029
#~ np.float32 : 0.800
#~ np.float64 : 0.866

最佳答案

半精度算术 (float16) 我猜是 numpy 必须“模拟”的东西,因为底层 C 语言(以及相应的处理器指令)中没有相应的类型。另一方面,使用 native 数据类型可以非常高效地完成单精度 (float32) 和 double (float64) 操作。

由于单精度运算的良好性能:现代处理器具有用于矢量化浮点运算(例如 AVX)的高效单元,因为它也是良好的多媒体性能所必需的。

关于Python Numpy 数据类型性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15340781/

相关文章:

python 错误: too many indices for array

PHP、MySQL性能和效率场景

c - 为什么执行时间会根据数据类型而变化?

python - 如何让 cProfile 只打印重要的功能?

python - 防止在每个请求上创建昂贵的对象

r - 如何每 50 列对 R 中的大型 data.table 进行子集化并查找字符行的模式

scala - Scala过滤器的收集类型

c++ - 类型的指针数组

python - Python 6 库的导入问题

c# - 与未存储的列表或数组一起使用时,foreach 循环是否工作得更慢?