Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: var parameter or result?

  1. #1

    var parameter or result?

    Hi, i was wondering, what's the best between:

    function sum(const a,b:TVector):TVector;
    function inv(const v:TVector):TVector;

    and

    procedure sum(var res:TVector; const a,b:TVector);
    procedure inv(var v:TVector);


    ?

    I mean, what's the faster and which is generally better ?

    thank you!
    If you save your data in a proprietary format, the owner of the format owns your data.
    <br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>

  2. #2

    var parameter or result?

    Hi MSX,

    I don't think that one of this is faster than the other.
    Maybe the procedure is a little bit faster, because it directly writes into the variable you give it. The function has to store the result and then copy it to the variable.

    Both have advantages for themselves, depending on what you want with that function/procedure.
    When implementing the procedure, you ALWAYS have to use a variable which stores the result. A function can do the same as the procedure, but it is not necessary to always process the result (maybe the function only results true or false for success), you can call it without processing the result, so in my opinion it's a little more "flexible".

    "Me, myself and I" always use function when I need ONE result, no matter if it is a value to be processed or only a boolean result like successful/unsuccesful.
    When more than one variable has to be processed/manipulated I use a procedure and give the variables to it.

    To come back to your example: In my opinion, this is a classic example for a FUNCTION. No one I know would code this in a procedure.

    Regards,
    Dirk
    <a href="http://www.greatgamesexperiment.com/game/Valgard/?utm_source=gge&amp;utm_medium=badge_game"><img border="0" alt="GGE" title="GGE" src="http://static.greatgamesexperiment.com/badge/game/valgard/gge400x56.png"></a>

  3. #3

    var parameter or result?

    i would use a procedure on inv and function on sum

  4. #4

    var parameter or result?

    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.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  5. #5

    var parameter or result?

    tux:
    This only makes sense when you don't need to work with the original value of the vector. If you need the inv just for a calculation then it would be better to use a function, since you can work with the inv AND with the original vector.

    example:

    function:
    do something with vector
    calculate something with inv of vector by calling function and use the result like a variable
    work again with original vector

    procedure:
    do something with vector
    calculate something with inv of vector
    work again with original vector <- here you have to invert the inv again, because you manipulated the vector in the procedure
    <a href="http://www.greatgamesexperiment.com/game/Valgard/?utm_source=gge&amp;utm_medium=badge_game"><img border="0" alt="GGE" title="GGE" src="http://static.greatgamesexperiment.com/badge/game/valgard/gge400x56.png"></a>

  6. #6

    var parameter or result?

    Quote Originally Posted by Huehnerschaender
    tux:
    This only makes sense when you don't need to work with the original value of the vector. If you need the inv just for a calculation then it would be better to use a function, since you can work with the inv AND with the original vector.
    i was assuming inv inverted the vector

  7. #7

    var parameter or result?

    I believe Savage is right... I remember reading in a book once (altho that was on C++) that for game development it was a good idea to pass most parameters to a procedure/function as a pointer. The book claimed this was faster, personally I think it will be only faster on structure and things larger than 4 bytes... but I could be wrong of course!
    Do it by the book, but be the author!
    <br />
    <br />Visit the Lion Productions website at:
    <br />http://lionprod.f2o.org

  8. #8

    var parameter or result?

    Hi everyone

    the answer to this question used to be held on www.optimalcode.com. It had a whole set of articles on optimization, but it's no longer available.

    The good news is that I have somewhere on my hard drive a copy of all the articles Do you think the origional authors would mind us posting them here??
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  9. #9

    var parameter or result?

    tux:
    Surely, this is not the best example. I understood the question general, not just to invert a vector which is mostly the simplest function one can write. I was assuming MSX wanted to know if in general it is better to do "things" using a function or using a procedure.

    Regards,

    Dirk :idea: :?: :!:
    <a href="http://www.greatgamesexperiment.com/game/Valgard/?utm_source=gge&amp;utm_medium=badge_game"><img border="0" alt="GGE" title="GGE" src="http://static.greatgamesexperiment.com/badge/game/valgard/gge400x56.png"></a>

  10. #10
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    var parameter or result?

    Well for me, speed issues aside, I'd rather have a function if I wanted to use it in your conditional statments, however if that is not important and I just wanted to alter some values(not really seeking any evaluation operation) then the procedure might work best in a sort of 'Black Box' type of functionality to it. It's up to how you want to handle the data coming out of it and what you want to do with it aswell. I think this might take some account for the speed difference in the bigger picture too. ie. other factors, running two sets of sum instead of one if you are doing a series of conditional checks, etc...

    Quote Originally Posted by technomage
    The good news is that I have somewhere on my hard drive a copy of all the articles Do you think the origional authors would mind us posting them here??
    I'd say go for it. If they mind we can always take it down. It would be an excellent resource to have. What format do you have it in? If it's HTML perhaps you could pretty it up some and email the zip to me. Since Zip functions are not enabled on the server you can't submit it as you would normally, but I can manually upload and add the listing of it for processing as it would normally. If there are errors however, I'll have to do the special by-pass again.

    So in short if you can get it in 1 zip file for me(whatever it consists of WWW viewable is best, PDF and the like acceptable), golden. I can fix it up to be listed in the Library.
    Jason McMillen
    Pascal Game Development
    Co-Founder





Page 1 of 2 12 LastLast

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
  •