Just to make sure everyone knows where the time difference comes from, I broke the code before every single step:
Whole code
%%timeit
tmp = np.array(a, dtype=np.uint8, copy=True)
tmp[tmp < 30] = 0
tmp[tmp > 224] = 0
10 loops, best 3: 21.6 ms per cycle
%%timeit
tmp = np.array(a, dtype=np.int8, copy=True)
tmp[tmp < 30] = 0
tmp[tmp > 224] = 0
100 cycles, best 3: 10.4 ms per cycle
So, the whole operation is faster, but let's look at the time spent in each installation operation :
%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 , 3: 19,3
100 , 3: 17,6
, int8:
100 , 3: 6,75
100 , 3: 4,36
, , , :
%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 , 3: 17,9
100 , 3: 16,2
int8:
100 , 3: 7,64
100 , 3: 4,3
int . :
%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 , 3: 4,25
100 , 3: 2,58
int8:
100 , 3: 4,26
100 , 3: 4,08
: dtype , , int. , numpy , all ( 235 -21 in int8) no. . uint 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
, , numpy , all/no element. False, numpy uint int.