[fpc-pascal] try..finally vs interfaces performance

Vincent Snijders vincent.snijders at gmail.com
Thu Apr 14 22:13:25 CEST 2016

2016-04-14 10:50 GMT+02:00 Michael Van Canneyt <michael at freepascal.org>:

> On Thu, 14 Apr 2016, Mazola Winstrol wrote:
> Recently i did some maintenance in a code of a colleague. I realized that
>> he designed several classes with interface support so he do not need to
>> protect the code blocks with try..finally to ensure that the instances are
>> released from memory.
>> Theoretically, which solution has higher performance? The traditional
>> approach (code protected try..finally) or classes with interfaces (no need
>> to protect the code. by otherside, classes with interfaces support have
>> lock operations)?
> It should be exactly the same. As soon as interfaces are used the compiler
> inserts an implicit try...finally in a procedure.
> You can see this if you step through the code with GDB, the execution point
> will jump to the end of the procedure and then back to the beginning...
I would venture, that manually adding try finally would give you better
performance than using interfaces at the cost of writing more code and
therefore less clear and more error-prone source.

If you use interfaces, the compiler also adds reference counting, which you
don't have if you free the objects at the right places.

It is not sure if you can measure the difference in performance though.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20160414/ebd603ee/attachment.html>

More information about the fpc-pascal mailing list