Friday, 15 February 2013

c# - Multiplication or if: what is more efficient? -


I have a binary image of the hand (size: 100x100), we can represent it as a matrix which is only 0 Or 1 value is an example:

example of a human hand binary image < / P>

assuming that I have the line double represent the linear image, and that we call it the image , I need to do the following :

  Double Conservator = 0; For (int j = 0; j and lt; image; height; ++ j) {accumulator + = image [j] * weight [j]; }   

In other words, I need to calculate the weighted sum of each pixel of the array, the image I weight to an array Indicates that there are double values, and it is used to load each pixel of the image.

Is the following code more efficient than the last one?

  Double Container = 0; (Int j = 0; j & lt; image.Length; ++ j) {if (image [j]! = 0) {creditor = = image [j] * weight [j]; }}    

The answer is - you do not really have to worry about it at this juncture. If your code is slow also is running, you can experiment with some customization, however, "very slow" is subjective.

Premature Optimization is the root cause of all evils .

Is easily readable, and can be easily maintained.

No comments:

Post a Comment