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

Thread: To good optimization?

  1. #11

    To good optimization?

    It's still passed by value. You'd have to explicitly pass the integers as pointers by using "var". Then the "X := X + 10;" line would modify the source variable. Otherwise just the local copy of the variable is modified.

  2. #12

    To good optimization?

    Quote Originally Posted by Setharian
    It's still passed by value. You'd have to explicitly pass the integers as pointers by using "var". Then the "X := X + 10;" line would modify the source variable. Otherwise just the local copy of the variable is modified.
    He's right. Compare these:
    [pascal]
    function MyPoint(X, Y: Integer): TMyPoint;
    begin
    X := X + 10;
    Result.X := X;
    Result.Y := Y;
    end;

    var
    X, Y: Integer;
    begin
    X := 10;
    Y := 20;
    MyPoint(X, Y);
    writeln(X);
    readln;
    end.
    [/pascal]
    [pascal]
    function MyPoint(var X, Y: Integer): TMyPoint;
    begin
    X := X + 10;
    Result.X := X;
    Result.Y := Y;
    end;

    var
    X, Y: Integer;
    begin
    X := 10;
    Y := 20;
    MyPoint(X, Y);
    writeln(X);
    readln;
    end.
    [/pascal]

    In the first case, X after passing to MyPoint still equals 10. But in the latter, X is passed as a reference, thus it equals 20.

    You can also use Assembler to boost up your program:
    [pascal]
    function MyPoint(X, Y: Integer): TMyPoint;
    // EAX contains address of X
    // EDX contains address of Y
    // ECX contains the result
    asm
    mov [ECX], EAX
    mov [ECX+4], EDX
    end;
    [/pascal]
    But I doubt you'll gain any speed using this.

  3. #13

    To good optimization?

    Quote Originally Posted by cronodragon
    Const is for string, record and array parameters.
    For anything with more than 4bytes in size
    From brazil (:

    Pascal pownz!

  4. #14

    To good optimization?

    Quote Originally Posted by arthurprs
    Quote Originally Posted by cronodragon
    Const is for string, record and array parameters.
    For anything with more than 4bytes in size
    Hmm, a method type (procedure of object) has 8 bytes and I haven't seen any tip about passing it with Const. What the Delphi Help says is that const should be used with structure types.

  5. #15

    To good optimization?

    Okay... thanx for the info... realy usefull
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  6. #16

    To good optimization?

    Quote Originally Posted by cronodragon
    Quote Originally Posted by arthurprs
    Quote Originally Posted by cronodragon
    Const is for string, record and array parameters.
    For anything with more than 4bytes in size
    Hmm, a method type (procedure of object) has 8 bytes and I haven't seen any tip about passing it with Const. What the Delphi Help says is that const should be used with structure types.
    Maybe its an "exeption" (not sure about this word)
    From brazil (:

    Pascal pownz!

  7. #17

    To good optimization?

    What const does is simply declare it as a constant in the function. The function can thus not alter it on syntax level, and the compiler then knows that all variables can be passed by reference. This is only a good thing if the parameter is very big, like a string or a big record

    With simple types you don't need it
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

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
  •