GetSizeWithinBounds

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

GetSizeWithinBounds

Postposted on Wed Mar 18, 2009 1:49 am

I just finished writing this algorithm.

It clocks in at about 8us, which is pretty good.
Can you think of any way to improve the performance?

One thing I don't like about it is the rather large if statements.
Do you think the sacrifice in clarity is worth the slimming of the code?

It makes sense for this method to take two Size objects.
I felt like this complicated the method call to much.
Would switching the parameters to two Size objects improve this method?

Code: Select all
public static Size GetSizeWithinBounds(Int32 height, Int32 width, Int32 max_height, Int32 max_width)
{
            Int32 new_height = height;
            Int32 new_width = width;
            Int32 height_difference = height - max_height;
            Int32 width_difference = width - max_width;

            if (height_difference > 0 && (width_difference <= 0 || height_difference >= width_difference))
            {
                new_height = max_height;
                new_width = (Int32)Math.Round((Double)new_height / ((Double)height / (Double)width), 0);
            }
            else if (width_difference > 0 && (height_difference <= 0 || width_difference >= height_difference))
            {
                new_width = max_width;
                new_height = (Int32)Math.Round((Double)new_width * ((Double)height / (Double)width), 0);
            }

            return new Size(new_width, new_height);
}
Chaospandion
Gerbil Team Leader
 
Posts: 201
Joined: Thu Mar 25, 2004 8:20 pm
Location: Pottstown, PA

Re: GetSizeWithinBounds

Postposted on Wed Mar 18, 2009 8:16 am

Smells like a homework question to me, but I'll post some feedback anyway...

To your comments about the if statements -- I don't really consider them all that bad. I expect that any attempt to "slim the code" may reduce the size of the ascii text representing the code, but I doubt it will significantly change the executed instructions.

Fastest way to boost performance? Get rid of the call to Math.Round() and the object creation (new) in the return. They are by far going to add more overhead than any other part of the routine.

--SS
SecretSquirrel
Gerbil Jedi
Gold subscriber
 
 
Posts: 1716
Joined: Tue Jan 01, 2002 7:00 pm
Location: The Colony, TX (Dallas suburb)

Re: GetSizeWithinBounds

Postposted on Wed Mar 18, 2009 9:10 am

I actually should have given a little background on this method.
It will be used to scale the size of an image to fit into a constant sized field.
This is not a homework problem unless you consider efforts to optimize multimillion dollar piece of crap homework.

The reason I used Math.Round is because I doubt I could make a function that is faster that offers the same functionality.
I need the result pixel size to be rounded so I limit the amount of stretching that gets done on an image.
Chaospandion
Gerbil Team Leader
 
Posts: 201
Joined: Thu Mar 25, 2004 8:20 pm
Location: Pottstown, PA

Re: GetSizeWithinBounds

Postposted on Wed Mar 18, 2009 9:11 am

Premature optimisation is the root of all evil


But I agree, this combined with your previous thread smell like homework.
notfred
Grand Gerbil Poohbah
 
Posts: 3736
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: GetSizeWithinBounds

Postposted on Wed Mar 18, 2009 6:26 pm

Chaospandion wrote:I actually should have given a little background on this method.
It will be used to scale the size of an image to fit into a constant sized field.
This is not a homework problem unless you consider efforts to optimize multimillion dollar piece of crap homework.

The reason I used Math.Round is because I doubt I could make a function that is faster that offers the same functionality.
I need the result pixel size to be rounded so I limit the amount of stretching that gets done on an image.


Given that background, I would consider further optimization of this routine to not be worth the effort. Focus on the actual pixel scaling routines. This function should be called, at most, once per image (or frame if you are doing video). The pixel scaling routine, whether in a separate subroutine (probably bad unless inlined) or just part of a loop is where you need to focus. On a small image (320x200) it will get called 64000 executed 64000 times, whereas this function gets executed once.

As notfred points out, the key to optimization is not how much you optimize, but where. Lets say for example you are able to speed up this function by 10x (an order of magnitude). In doing so, you have reduce the time to process an image by 7.2us. Lets assume for this example that the pixel scaling takes 1us per pixel and you are able reduce its time by only 5% (0.2us). For a 320x200 image, optimizing the function you gave by a factor of 10 will decrease the image processing time by 0.01% whereas reducing the pixel scaling time will reduce image processing time by 5.00%. This ignores all the other processing time to scale the image, but the point is still valid.

--SS
SecretSquirrel
Gerbil Jedi
Gold subscriber
 
 
Posts: 1716
Joined: Tue Jan 01, 2002 7:00 pm
Location: The Colony, TX (Dallas suburb)

Re: GetSizeWithinBounds

Postposted on Thu Mar 19, 2009 12:25 am

Wisdom is much easier to digest with a concise and easier to understand example.

Thank you.
Chaospandion
Gerbil Team Leader
 
Posts: 201
Joined: Thu Mar 25, 2004 8:20 pm
Location: Pottstown, PA


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest