如何(快速)将float转换为Byte?

时间:2015-05-01 03:04:09

标签: ios objective-c

我有一个处理位图中所有像素的方法(表示为Byte数组)。对于每个像素,我使用float对每个R,G和B值进行相当复杂的计算,以存储计算值,直到最终将其转换回Byte(仅使用强制转换,我和# 39;确定float中的值始终为255.0或更小。

在尝试优化此方法时,我惊讶地发现大约80%的总处理时间来自于将R,G和B的三个float值转换为Byte对应的。

有没有任何超快速的方法(例如):

float Rtotal = 123.7;
float Gtotal = 7.3;
float Btotal = 221.3;

Byte Rsource = (Byte)Rtotal;
Byte Gsource = (Byte)Gtotal;
Byte Bsource = (Byte)Btotal;

1 个答案:

答案 0 :(得分:3)

好的,这有点奇怪。如果我只做这个改变:

float Rtotal = 123.7;
float Gtotal = 7.3;
float Btotal = 221.3;

Byte Rsource = (int)Rtotal;
Byte Gsource = (int)Gtotal;
Byte Bsource = (int)Btotal;

施法者(Byte)造成的额外时间消失了。我的猜测是,编译器正在向(Byte)强制转换添加某种边界检查以确保它在字节的有效范围内,而如果强制转换为{{1 }}

相关问题