[fpc-pascal] with statement using mulltiple objects
Flávio Etrusco
flavio.etrusco at gmail.com
Tue Sep 16 22:42:26 CEST 2014
On Mon, Sep 15, 2014 at 2:55 AM, Sven Barth <pascaldragon at googlemail.com> wrote:
> On 14.09.2014 18:05, Philippe wrote:
>>
>
> Take this example:
>
> === code begin ===
>
(...)
>
> procedure TestWith;
> var
> p: PTest;
> begin
> New(p);
>
> with p^ do begin
> Prop1 := 42;
> Prop2 := 21;
> end;
>
> Dispose(p);
> end;
>
> procedure TestWithout;
> var
> p: PTest;
> begin
> New(p);
>
> p^.Prop1 := 42;
> p^.Prop2 := 21;
>
> Dispose(p);
> end;
>
> begin
>
> end.
>
> === code end ===
>
> This is the relevant code generated for TestWith:
>
> === asm begin ===
(...)
> === asm end ===
>
> As you can see the expression p^ is only evaluated once in the TestWith case
> while it's evaluated twice in the TestWithout one. So it's only minimally
> faster in this example (one less memory access), but if you use enough
> members of TTest it a more or less tight loop it might even be noticeable.
>
> Regards,
> Sven
I expected FPC would optimize this (with just -O1) :-(
-Flávio
More information about the fpc-pascal
mailing list