gpt4 book ai didi

python - np.uint8 和 np.int8 的不同执行时间

转载 作者:太空宇宙 更新时间:2023-11-03 11:47:31 25 4
gpt4 key购买 nike

我有这段代码:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import time
import numpy as np

for t in [np.uint8, np.int8]:
a=np.empty([480, 640], t)
v=[10, 245]
for y in range(480):
for x in range(640):
a[y, x]=v[x&1] # 50%=10, 50%=245
t1=time.clock()
a[a<32]=0
a[a>224]=0
t2=time.clock()
print("%2.3f ms"%((t2-t1)*1000), a.dtype)

我得到了这个输出:

3.162 ms uint8
0.329 ms int8

为什么这是a[a<32]=0如果它在有符号数组上运行,速度会快十倍?

有没有办法在无符号数组上加速它?

是的......更多样本也是如此:

for t in [np.int8, np.uint8]:
a=np.empty([480, 640], t)
v=[10, 245]
for y in range(480):
for x in range(640):
a[y, x]=v[x&1] # 50%=10, 50%=235

t1=time.process_time()
for l in range(1000):
b=1*a # deep copy
b[b<32]=0
b[b>224]=0
t2=time.process_time()
print("%5.4f ms"%((t2-t1)*1000), a.dtype)

结果是这样的:

328.0701 ms int8
3081.5300 ms uint8

最佳答案

为了确保每个人都知道时差的来源,我将代码分解为每个单独的步骤:

完整代码

%%timeit 
tmp = np.array(a, dtype=np.uint8, copy=True)
tmp[tmp < 30] = 0
tmp[tmp > 224] = 0

10 loops, best of 3: 21.6 ms per loop

%%timeit 
tmp = np.array(a, dtype=np.int8, copy=True)
tmp[tmp < 30] = 0
tmp[tmp > 224] = 0

100 loops, best of 3: 10.4 ms per loop

是的,整个操作速度更快,但让我们看看每个设置操作所花费的时间:

%timeit tmp = np.array(a, dtype=np.uint8, copy=True); tmp[tmp < 30] = 0
tmp = np.array(a, dtype=np.uint8, copy=True)
tmp[tmp < 30] = 0
%timeit tmp2 = np.array(tmp, copy=True); tmp2[tmp2 > 224] = 0

100 loops, best of 3: 19.3 ms per loop

100 loops, best of 3: 17.6 ms per loop

因此对于 int8,每个设置都需要相同的时间:

100 loops, best of 3: 6.75 ms per loop

100 loops, best of 3: 4.36 ms per loop

让我们看看如果我们只根据索引创建一个新 View 会发生什么:

%timeit tmp = np.array(a, dtype=np.uint8, copy=True); _ = tmp[tmp < 30]
tmp = np.array(a, dtype=np.uint8, copy=True)
tmp[tmp < 30] = 0
%timeit tmp2 = np.array(tmp, copy=True); _ = tmp2[tmp2 > 224]

100 loops, best of 3: 17.9 ms per loop

100 loops, best of 3: 16.2 ms per loop

int8 :

100 loops, best of 3: 7.64 ms per loop

100 loops, best of 3: 4.3 ms per loop

静止int是比较快的。那么如何创建 bool 掩码:

%timeit tmp = np.array(a, dtype=np.uint8, copy=True); _ = tmp < 30
tmp = np.array(a, dtype=np.uint8, copy=True)
tmp[tmp < 30] = 0
%timeit tmp2 = np.array(tmp, copy=True); _ = tmp2 > 224

100 loops, best of 3: 4.25 ms per loop

100 loops, best of 3: 2.58 ms per loop

int8 :

100 loops, best of 3: 4.26 ms per loop

100 loops, best of 3: 4.08 ms per loop

长话短说:在创建 bool 掩码时,dtype 没有太大区别,但如果您使用 bool 掩码创建数据的新 View ,则使用 int 会快得多。 .但这只是一种错觉,因为实际上 numpy 看到它在第一个操作中访问了 所有 元素(因为 235 get 在 int8 中被转换为 -21)并且 no 中的元素第二次手术。与 uint both 操作的掩码同时包含 True 和 False(混合)。

总结:因此 numpy 可以并且确实优化了获取和设置数组的所有/没有元素。

在您提到的评论中,使用 v=[10,100] 会更快对于 uint但在我的电脑上,设置与上面相同,两者大致相同:

uint: 10 loops, best of 3: 21.7 ms per loop
int: 10 loops, best of 3: 23.2 ms per loop

这是因为现在第一个操作有一个混合的 bool 掩码,而 numpy 不能像设置所有/没有元素那样优化它。但是第二个操作有一个 bool 掩码只有 False所以 numpy 会为 uint 和 int 跳过这个。

关于python - np.uint8 和 np.int8 的不同执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35728936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com