-5

I have a binary image (size: 100x100) of a hand, that we can represent as a matrix composed only by 0 or 1 values. This is an example:

Example of a binary image of a human hand

Assuming that I have an array of double representing the linearized image, and that we call it image, I need to perform the following operations:

double accumulator = 0;
for (int j = 0; j < image.Length; ++j)
{
    accumulator += image[j] * weights[j];
}

In other words, I need to calculate the weighted sum of each pixel of the image array. weights represents an array that contains double values, and it is used to weight each pixel of the image.

Is the following code more efficient than the previous one?

double accumulator = 0;
for (int j = 0; j < image.Length; ++j)
{
    if (image[j] != 0)
    {
        accumulator += image[j] * weights[j];
    }
}
4

1 回答 1

4

答案是——你真的不需要在这个时刻担心这个。如果您的代码运行速度太慢,那么您可以尝试一些优化。然而,“太慢”是主观的。

过早的优化是万恶之源

正如 angelatlarge 指出的那样,目前 - 使用易于阅读且易于维护的内容。

于 2013-03-26T18:53:01.513 回答