Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: var parameter or result?

  1. #11

    var parameter or result?

    Well if you only need one result you can use a function, it's more clear, look at WinAPI for example, it's full of functions, even procedures have a result, it's always nil/null/void
    But if you use the register calling convention and the first 3 params are integers, you can get some speed since they are stored in registers (see Delphi docs), however i think on most OSes you only have cdecl so you don't get any extra speed by using this method
    The future must be... Fast and OpenSource so...
    <br />Think Open and Lightning Fast!

  2. #12

    var parameter or result?

    If function returns simple number it's placed in EAX register. So function will be more effective then passing pointer to variable and dereferencing it in a procedure.
    But if function returns complex record it's IIRC allocated on stack with futher copy. But in case of procedure you pass real pointer to it (VAR parameter) - so it will be win in this comparision.
    On other side in Delphi9 and FPC.1.9.x one can declare INLINE functions returning complex results and in this case after optimization compiler can produce more or less equal code for function and procedure ( :!: I have not investigated this really :!.
    There are only 10 types of people in this world; those who understand binary and those who don't.

  3. #13

    var parameter or result?

    The "ugly" way with records/functions is to use pointers as results.
    EG:

    function doit(const a: TMyRecord): PMyRecord;

    I personaly use functions only where I do require if-ing the results, otherwise I use procedures packed with consts and vars(const is very smart when it comes to passing).
    Feel the power of Open Source.
    <br />Feel the power of Free Pascal.

  4. #14

    var parameter or result?

    Quote Originally Posted by savage
    I believe that the number of parameters passed to a function/parameter may have a speed impact, so try and limit them to 3 max ( I think that is right ), as it will then use the CPU registers.

    I am sure I read this somewhere, but someone may need to confirm this.
    IIRC it is 4, but self takes one too

    However if TVector is not a basic type (pointer, integer, anything that fits in a register), it will become an implicit parameter anyway, so moving it has no effect.

    I think VAR is much better if the value is a complex or automated type, and that it doesn't matter that much.

    Stack access is a lot faster on P6 and later cores. (keep in mind Delphi was designed for 486/P-I originally)

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •