可这code进行优化? [英] Can this code be optimised?
问题描述
我有一些图像处理code来遍历2多维字节数组(大小相同)。它从源数组的值,再对其进行计算,然后将结果存储在另一个数组。
INT xSize = ResultImageData.GetLength(0);
INT ySize = ResultImageData.GetLength(1);
为(中间体X = 0 X - 其中; xSize; X ++)
{
对于(INT Y = 0; Y< ySize; Y ++)
{
ResultImageData [X,Y] =(字节)((CurrentImageData [X,Y] * AlphaValue)+
(AlphaImageData [X,Y] * OneMinusAlphaValue));
}
}
环路目前需要11毫秒〜,我认为主要是由于访问字节数组值作为计算pretty的简单(2乘法和加法1)。
有什么我可以做,以加快这?这是我的程序的时间关键部分,这code被称为每秒80-100次,所以任何速度上涨,但是小会有所作为。此外,在时刻xSize = 768和ySize = 576,但这样会在将来增加
更新:感谢 Guffa (见回答以下),以下$ C $ Ç为我节省了每个循环4-5ms。虽然它的不安全的code。
INT大小= ResultImageData.Length;
INT计数器= 0;
不安全
{
固定(BYTE * R = ResultImageData,C = CurrentImageData,A = AlphaImageData)
{
而(尺寸大于0)
{
*(R +计数器)=(字节)(*(C +计数器)* AlphaValue +
*(A +计数器)* OneMinusAlphaValue);
反++;
尺寸 - ;
}
}
}
要得到任何真正的speadup此code,你就需要使用指针访问数组时,要删除所有指标的计算和边界检查。
INT大小= ResultImageData.Length;
不安全
{
固定(BYTE * RP = ResultImageData,CP = CurrentImageData,AP = AlphaImageData)
{
字节* R = RP;
字节* C = CP;
字节* A = AP;
而(尺寸大于0)
{
* R =(字节)(* C * AlphaValue + * A * OneMinusAlphaValue);
- [R ++;
C ++;
A ++;
尺寸 - ;
}
}
}
编辑:
固定变量不能改变,所以我加了code的指针复制到可以改变的新指针。
I have some image processing code that loops through 2 multi-dimensional byte arrays (of the same size). It takes a value from the source array, performs a calculation on it and then stores the result in another array.
int xSize = ResultImageData.GetLength(0);
int ySize = ResultImageData.GetLength(1);
for (int x = 0; x < xSize; x++)
{
for (int y = 0; y < ySize; y++)
{
ResultImageData[x, y] = (byte)((CurrentImageData[x, y] * AlphaValue) +
(AlphaImageData[x, y] * OneMinusAlphaValue));
}
}
The loop currently takes ~11ms, which I assume is mostly due to accessing the byte arrays values as the calculation is pretty simple (2 multiplications and 1 addition).
Is there anything I can do to speed this up? It is a time critical part of my program and this code gets called 80-100 times per second, so any speed gains, however small will make a difference. Also at the moment xSize = 768 and ySize = 576, but this will increase in the future.
Update: Thanks to Guffa (see answer below), the following code saves me 4-5ms per loop. Although it is unsafe code.
int size = ResultImageData.Length;
int counter = 0;
unsafe
{
fixed (byte* r = ResultImageData, c = CurrentImageData, a = AlphaImageData)
{
while (size > 0)
{
*(r + counter) = (byte)(*(c + counter) * AlphaValue +
*(a + counter) * OneMinusAlphaValue);
counter++;
size--;
}
}
}
To get any real speadup for this code you would need to use pointers to access the arrays, that removes all the index calculations and bounds checking.
int size = ResultImageData.Length;
unsafe
{
fixed(byte* rp = ResultImageData, cp = CurrentImageData, ap = AlphaImageData)
{
byte* r = rp;
byte* c = cp;
byte* a = ap;
while (size > 0)
{
*r = (byte)(*c * AlphaValue + *a * OneMinusAlphaValue);
r++;
c++;
a++;
size--;
}
}
}
Edit:
Fixed variables can't be changed, so I added code to copy the pointers to new pointers that can be changed.
这篇关于可这code进行优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!