I am using Delphi 2007 and working on some presentation software. The current module I am working on is the transition filter for video. The transition code I am using (TPicShow’s PSEffects unit) requires and X and a Y value based on the dimensions of the frame and the progress of the transition. Here is the code
Type
TPercent = 0..100;
var
ATo : TBitmap; //
Prog : Integer; //Progress of the transition
if ATo.Width >= ATo.Height then
begin
X := MulDiv(ATo.Width, Prog, High(TPercent));
Y := MulDiv(X, ATo.Height, ATo.Width);
end
else
begin
Y := MulDiv(ATo.Height, Prog, High(TPercent));
X := MulDiv(Y, ATo.Width, ATo.Height);
end;
I am trying to optimize this and saw that I could save the calculations that would be constant (until the dimensions of ATo change) and remove 2 division calculations each frame.
So it would be something like
{All of these are calculated when the dimensions of ATo Change}
WDP : real; // width divided by High(TPercent)
HDW : real; // Height divided by width
HDP : real; // Height divided by High(TPercent)
WDH : real; // Width divided by Height
if ATo.Width >= ATo.Height then
begin
X := Trunc(WDP * Prog);
Y := Trunc(HDW * X);
end
else
begin
Y := Trunc(HDP * Prog);
X := Trunc(WDH * Y);
end;
It sounds good but not having the actual code of MulDiv I cant be sure. If it simply does (very simplified)
MulDiv(a,b,c : Integer)
begin
Round((A*B)/C);
end
Then I know my change will be more efficient, however if MulDiv does anything very cool with optimizing the function (Which I’d thing it might) then I’m not sure if my change would net me anything.
Would my change be more efficient?
EDIT: I have not yet implemented this, I’m just entertaining the notion.
I’d be very surprised if calls to MulDiv, which is implemented using integer operations, were inefficient and the source of your performance problems. Have you timed your program? Have you used a profiler to identify the hot spots in your app?
Personally I think it is unlikely that a switch from integer to double precision floating point operations is likely to yield performance improvements.
In any case, my guess would be that you have other code which you call after the code you have shown, which uses
XandYand which consumes orders of magnitude more CPU than this little snippet. You presumably don’t calculateXandYand then discard them: what do you do with them?EDIT: The Wine implementation of MulDiv is presumably very close to the Windows one, and the guts of that is so: