PDA

View Full Version : var parameter or result?



{MSX}
24-01-2005, 03:32 PM
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!

Huehnerschaender
24-01-2005, 05:12 PM
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

tux
24-01-2005, 05:21 PM
i would use a procedure on inv and function on sum :)

savage
24-01-2005, 05:46 PM
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.

Huehnerschaender
24-01-2005, 07:23 PM
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

tux
24-01-2005, 08:19 PM
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 :)

TheLion
24-01-2005, 09:32 PM
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! ;)

technomage
24-01-2005, 10:23 PM
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??

Huehnerschaender
25-01-2005, 12:45 AM
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: :?: :!: :wink:

WILL
25-01-2005, 12:48 AM
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...


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.

Lightning
25-01-2005, 03:47 PM
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 :(

Clootie
25-01-2005, 11:05 PM
If function returns simple number it's placed in EAX register. :D 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 :!:).

Almindor
26-01-2005, 11:10 AM
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).

marcov
27-01-2005, 11:47 AM
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)