| View previous topic :: View next topic |
| Author |
Message |
Dennis Guest
|
Posted: Tue Oct 04, 2005 9:29 am Post subject: Code Optimization Stack Pointer Manipulation |
|
|
Hi
Can anybody tell me why the compiler emits this code
pop ecx
pop ecx
and not
add esp,8
?
The pop ecx /pop ecx code looks weird and slow to me while add esp,8 is very
natural fast and simple.
popping a register involves a copy of data from the stack into the register.
This is repeated twice and is slower and bigger than just one add.
Best regards
Dennis
|
|
| Back to top |
|
 |
Per Larsen Guest
|
Posted: Tue Oct 04, 2005 9:41 am Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
The double-pop is one byte shorter than the add - my guess is that's the
reason and that there wasn't any significant difference in speed when this
was originally done. I agree that it looks weird, FWIW.
- Per
|
|
| Back to top |
|
 |
Dennis Guest
|
Posted: Tue Oct 04, 2005 9:48 am Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
Hi Per
Thanks for explaining.
Should we recommend it being changed?
Best regards
Dennis
|
|
| Back to top |
|
 |
Per Larsen Guest
|
Posted: Tue Oct 04, 2005 1:11 pm Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
"Dennis" <marianndkc (AT) home3 (DOT) gvdnet.dk> wrote
| Quote: | Hi Per
Thanks for explaining.
Should we recommend it being changed?
|
If we can prove it's significantly faster, yes - otherwise no.
- Per
|
|
| Back to top |
|
 |
Dennis Guest
|
Posted: Tue Oct 04, 2005 1:21 pm Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
Hi Per
I will do some benchmarking but I do not think that it is significant
slower.
I any circumstances it is an "Extreme cornercase" priority report - if made.
Best regards
Dennis
|
|
| Back to top |
|
 |
Dennis Guest
|
Posted: Tue Oct 04, 2005 1:32 pm Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
Hi
Somebody help me benchmark
function FirstOrderPolynomialBASM1(X : Double) : Double;
const
A : Double = 2;
B : Double = 3;
asm
add esp,-$08
//Result := A*X + B;
fld A
fmul qword ptr [ebp+$08]
fadd B
fstp qword ptr [ebp-$08]
wait
fld qword ptr [ebp-$08]
pop ecx
pop ecx
end;
versus
function FirstOrderPolynomialBASM2(X : Double) : Double;
const
A : Double = 2;
B : Double = 3;
asm
add esp,-$08
//Result := A*X + B;
fld A
fmul qword ptr [ebp+$08]
fadd B
fstp qword ptr [ebp-$08]
wait
fld qword ptr [ebp-$08]
//pop ecx
//pop ecx
add esp,$08
end;
I use this simple benchmark
procedure TTimingForm.Button1Click(Sender: TObject);
var
StartTime, EndTime, RunTime : TDateTime;
I : Integer;
Y, X : Double;
const
MAX : Integer = 1700000000;
begin
X := 5;
StartTime := Time;
for I := 1 to MAX do
begin
Y := FirstOrderPolynomialBASM1(X);
//Y := FirstOrderPolynomialBASM2(X);
end;
EndTime := Time;
RunTime := EndTime - StartTime;
RunTimeEdit.Text := TimeToStr(RunTime);
YEdit.Text := FloatToStr(Y);
end;
Best regards
Dennis
|
|
| Back to top |
|
 |
Dennis Guest
|
Posted: Tue Oct 04, 2005 1:37 pm Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
Hi
I get
FirstOrderPolynomialBASM1 // 53 sec
FirstOrderPolynomialBASM2 // 44 sec
On Opteron 240.
But we should not jump to any conclusion until we have more benchmark
results. The benchmark does not take care of branch target alignment or data
alignment. More compilations and runs are needed.
Best regards
Dennis
|
|
| Back to top |
|
 |
Dennis Guest
|
Posted: Tue Oct 04, 2005 1:42 pm Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
I get
FirstOrderPolynomialBASM1 // 31 sec
FirstOrderPolynomialBASM2 // 27 sec
On Dothan 1733.
Best regards
Dennis
|
|
| Back to top |
|
 |
Florent Ouchet Guest
|
Posted: Tue Oct 04, 2005 4:38 pm Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
Hi,
I get
FirstOrderPolynomialBASM1 // 1 min 18
FirstOrderPolynomialBASM2 // 1 min 19
on a P3M @ 866MHz
Results are really close.
Regards,
Florent
|
|
| Back to top |
|
 |
Dennis Guest
|
Posted: Tue Oct 04, 2005 6:10 pm Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
Hi Florent
Could you upload your exe's to attachments?
And the source.
Then I will try run them here too.
Best regards
Dennis
|
|
| Back to top |
|
 |
Florent Ouchet Guest
|
Posted: Tue Oct 04, 2005 6:59 pm Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
Dennis wrote:
| Quote: | Could you upload your exe's to attachments?
And the source.
|
I use the one you provided, will little modifications.
this simple benchmark is really easy to use. I don't take care of
function alignment.
Adding the SetThreadPriority increases the differencies :
25s for add
27s for pop pop
(I reduced the number of loop).
Regards,
Florent
|
|
| Back to top |
|
 |
Dennis Guest
|
Posted: Tue Oct 04, 2005 7:37 pm Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
Hi Florent
I would like the exe's. They migth be aligned diffrently from the ones I
made.
Best regards
Dennis
|
|
| Back to top |
|
 |
Dennis Guest
|
Posted: Tue Oct 04, 2005 7:40 pm Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
Just found it.
14 versus 14 seconds on my Opteron.
Best regards
Dennis
|
|
| Back to top |
|
 |
Florent Ouchet Guest
|
Posted: Tue Oct 04, 2005 7:45 pm Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
Dennis wrote:
| Quote: | I would like the exe's. They migth be aligned diffrently from the ones I
made.
|
There are big differences between benchmarking using the debug and
debugging in release. That is the only idea that comes to me.
The exe is not difficult to find I inserted exe and project/source
files in the zip file.
Florent
|
|
| Back to top |
|
 |
Dennis Guest
|
Posted: Tue Oct 04, 2005 7:49 pm Post subject: Re: Code Optimization Stack Pointer Manipulation |
|
|
Hi Florent
Yes, but you did not write here that you uploaded anything ;-)
I think we need a better benchmark. I will try to make one tomorrow based on
our Fastcode Template B&V.
Best regards
Dennis
|
|
| Back to top |
|
 |
|